第12章 自定义样式

在本章中,我们将学习如何自定义RecordRTC的界面样式,包括录制控件、预览界面和进度指示器等。

12.1 录制控件样式定制

自定义录制控件的外观和交互效果:

录制控件样式

<!-- HTML结构 -->
<div class="recording-controls">
    <button id="startBtn" class="record-btn start-btn">
        <span class="btn-icon">●</span>
        <span class="btn-text">开始录制</span>
    </button>
    <button id="pauseBtn" class="record-btn pause-btn" style="display: none;">
        <span class="btn-icon">⏸</span>
        <span class="btn-text">暂停</span>
    </button>
    <button id="resumeBtn" class="record-btn resume-btn" style="display: none;">
        <span class="btn-icon">▶</span>
        <span class="btn-text">继续</span>
    </button>
    <button id="stopBtn" class="record-btn stop-btn" style="display: none;">
        <span class="btn-icon">■</span>
        <span class="btn-text">停止</span>
    </button>
</div>

<!-- CSS样式 -->
<style>
.recording-controls {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin: 20px 0;
    flex-wrap: wrap;
}

.record-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.record-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

.record-btn:active {
    transform: translateY(0);
}

.start-btn {
    background-color: #ff4757;
    color: white;
}

.start-btn:hover {
    background-color: #ff2e43;
}

.pause-btn {
    background-color: #ffa502;
    color: white;
}

.pause-btn:hover {
    background-color: #e69500;
}

.resume-btn {
    background-color: #2ed573;
    color: white;
}

.resume-btn:hover {
    background-color: #26c466;
}

.stop-btn {
    background-color: #57606f;
    color: white;
}

.stop-btn:hover {
    background-color: #48515f;
}

.btn-icon {
    font-size: 18px;
}

.btn-text {
    font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .record-btn {
        padding: 10px 15px;
        font-size: 14px;
    }
    
    .btn-icon {
        font-size: 16px;
    }
    
    .btn-text {
        font-size: 14px;
    }
}
</style>

12.2 预览界面定制

自定义视频预览界面的样式:

预览界面样式

<!-- HTML结构 -->
<div class="video-preview-container">
    <div class="preview-header">
        <h3>录制预览</h3>
        <div class="preview-controls">
            <button id="flipCameraBtn" class="control-btn">
                <span class="icon">🔄</span>
            </button>
            <button id="toggleMirrorBtn" class="control-btn">
                <span class="icon">🪞</span>
            </button>
        </div>
    </div>
    <div class="video-wrapper">
        <video id="previewVideo" class="preview-video" autoplay muted playsinline></video>
        <div class="recording-indicator" id="recordingIndicator">
            <div class="recording-dot"></div>
            <span class="recording-text">录制中</span>
        </div>
    </div>
    <div class="preview-info">
        <div class="info-item">
            <span class="info-label">分辨率:</span>
            <span id="resolutionInfo" class="info-value">1280×720</span>
        </div>
        <div class="info-item">
            <span class="info-label">帧率:</span>
            <span id="framerateInfo" class="info-value">30 fps</span>
        </div>
        <div class="info-item">
            <span class="info-label">时长:</span>
            <span id="durationInfo" class="info-value">00:00</span>
        </div>
    </div>
</div>

<!-- CSS样式 -->
<style>
.video-preview-container {
    background: #f8f9fa;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    margin: 20px 0;
}

.preview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    background: #ffffff;
    border-bottom: 1px solid #e9ecef;
}

.preview-header h3 {
    margin: 0;
    color: #495057;
    font-size: 18px;
}

.preview-controls {
    display: flex;
    gap: 10px;
}

.control-btn {
    background: #e9ecef;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
}

.control-btn:hover {
    background: #dee2e6;
    transform: scale(1.1);
}

.video-wrapper {
    position: relative;
    background: #000;
}

.preview-video {
    width: 100%;
    height: auto;
    display: block;
    max-height: 60vh;
    object-fit: contain;
}

.recording-indicator {
    position: absolute;
    top: 15px;
    left: 15px;
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(0,0,0,0.7);
    padding: 8px 12px;
    border-radius: 20px;
    color: white;
    font-size: 14px;
}

.recording-dot {
    width: 10px;
    height: 10px;
    background: #ff4757;
    border-radius: 50%;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0% { opacity: 1; }
    50% { opacity: 0.5; }
    100% { opacity: 1; }
}

.preview-info {
    display: flex;
    justify-content: space-around;
    padding: 15px 20px;
    background: #ffffff;
    border-top: 1px solid #e9ecef;
    flex-wrap: wrap;
    gap: 10px;
}

.info-item {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.info-label {
    font-size: 12px;
    color: #6c757d;
    margin-bottom: 4px;
}

.info-value {
    font-size: 14px;
    font-weight: 500;
    color: #495057;
}
</style>

12.3 进度指示器定制

创建自定义的录制进度指示器:

进度指示器样式

<!-- HTML结构 -->
<div class="recording-progress">
    <div class="progress-header">
        <span class="progress-label">录制进度</span>
        <span id="progressTime" class="progress-time">00:00</span>
    </div>
    <div class="progress-container">
        <div class="progress-bar">
            <div class="progress-fill" id="progressFill"></div>
        </div>
    </div>
    <div class="progress-stats">
        <div class="stat-item">
            <span class="stat-label">文件大小:</span>
            <span id="fileSize" class="stat-value">0 KB</span>
        </div>
        <div class="stat-item">
            <span class="stat-label">比特率:</span>
            <span id="bitrate" class="stat-value">0 kbps</span>
        </div>
    </div>
</div>

<!-- CSS样式 -->
<style>
.recording-progress {
    background: white;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    margin: 20px 0;
}

.progress-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
}

.progress-label {
    font-weight: 600;
    color: #495057;
}

.progress-time {
    font-family: 'Courier New', monospace;
    color: #6c757d;
}

.progress-container {
    width: 100%;
    height: 12px;
    background: #e9ecef;
    border-radius: 6px;
    overflow: hidden;
    margin-bottom: 15px;
}

.progress-bar {
    width: 100%;
    height: 100%;
    background: #e9ecef;
    border-radius: 6px;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
    border-radius: 6px;
    width: 0%;
    transition: width 0.3s ease;
    position: relative;
}

.progress-fill::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
    animation: progressShine 2s infinite;
}

@keyframes progressShine {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}

.progress-stats {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 15px;
}

.stat-item {
    display: flex;
    flex-direction: column;
}

.stat-label {
    font-size: 12px;
    color: #6c757d;
    margin-bottom: 4px;
}

.stat-value {
    font-size: 14px;
    font-weight: 500;
    color: #495057;
    font-family: 'Courier New', monospace;
}
</style>

12.4 主题定制

创建不同的主题样式:

主题定制

<!-- 主题切换按钮 -->
<div class="theme-selector">
    <button class="theme-btn" data-theme="light">明亮主题</button>
    <button class="theme-btn" data-theme="dark">暗黑主题</button>
    <button class="theme-btn" data-theme="blue">蓝色主题</button>
    <button class="theme-btn" data-theme="green">绿色主题</button>
</div>

<!-- CSS主题样式 -->
<style>
/* 默认主题变量 */
:root {
    --primary-color: #007bff;
    --secondary-color: #6c757d;
    --success-color: #28a745;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --info-color: #17a2b8;
    --light-bg: #f8f9fa;
    --dark-bg: #343a40;
    --text-color: #212529;
    --border-color: #dee2e6;
}

/* 暗黑主题 */
[data-theme="dark"] {
    --primary-color: #0d6efd;
    --secondary-color: #6c757d;
    --success-color: #198754;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --info-color: #0dcaf0;
    --light-bg: #212529;
    --dark-bg: #f8f9fa;
    --text-color: #f8f9fa;
    --border-color: #495057;
}

/* 蓝色主题 */
[data-theme="blue"] {
    --primary-color: #0d6efd;
    --secondary-color: #6c757d;
    --success-color: #198754;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --info-color: #0dcaf0;
    --light-bg: #e7f1ff;
    --dark-bg: #0a2540;
    --text-color: #0a2540;
    --border-color: #b3d7ff;
}

/* 绿色主题 */
[data-theme="green"] {
    --primary-color: #198754;
    --secondary-color: #6c757d;
    --success-color: #198754;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --info-color: #0dcaf0;
    --light-bg: #f0fff4;
    --dark-bg: #0a4024;
    --text-color: #0a4024;
    --border-color: #b3e6c4;
}

/* 主题按钮样式 */
.theme-selector {
    display: flex;
    gap: 10px;
    margin: 20px 0;
    flex-wrap: wrap;
}

.theme-btn {
    padding: 8px 16px;
    border: 2px solid var(--border-color);
    background: var(--light-bg);
    color: var(--text-color);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.theme-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.theme-btn.active {
    border-color: var(--primary-color);
    background: var(--primary-color);
    color: white;
}

/* 应用主题到录制控件 */
[data-theme] .record-btn {
    background: var(--primary-color);
    color: white;
    border: none;
}

[data-theme="dark"] .record-btn {
    background: var(--primary-color);
}

[data-theme="blue"] .record-btn {
    background: var(--primary-color);
}

[data-theme="green"] .record-btn {
    background: var(--primary-color);
}

/* JavaScript主题切换 */
<script>
document.querySelectorAll('.theme-btn').forEach(button => {
    button.addEventListener('click', function() {
        const theme = this.getAttribute('data-theme');
        
        // 移除所有主题类
        document.documentElement.removeAttribute('data-theme');
        
        // 应用新主题
        if (theme !== 'light') {
            document.documentElement.setAttribute('data-theme', theme);
        }
        
        // 更新活动按钮状态
        document.querySelectorAll('.theme-btn').forEach(btn => {
            btn.classList.remove('active');
        });
        this.classList.add('active');
    });
});
</script>
</style>

12.5 响应式设计

确保界面在不同设备上都能良好显示:

响应式设计

<!-- 响应式CSS -->
<style>
/* 大屏幕设备 */
@media (min-width: 1200px) {
    .recording-controls {
        max-width: 600px;
        margin: 30px auto;
    }
    
    .video-preview-container {
        max-width: 800px;
        margin: 30px auto;
    }
    
    .preview-video {
        max-height: 70vh;
    }
}

/* 平板设备 */
@media (min-width: 768px) and (max-width: 1199px) {
    .recording-controls {
        max-width: 90%;
        margin: 25px auto;
    }
    
    .video-preview-container {
        max-width: 95%;
        margin: 25px auto;
    }
    
    .preview-video {
        max-height: 60vh;
    }
    
    .progress-stats {
        gap: 20px;
    }
}

/* 手机设备 */
@media (max-width: 767px) {
    .recording-controls {
        flex-direction: column;
        align-items: center;
        gap: 15px;
        padding: 0 15px;
    }
    
    .record-btn {
        width: 100%;
        max-width: 300px;
        justify-content: center;
    }
    
    .video-preview-container {
        margin: 20px 10px;
        border-radius: 8px;
    }
    
    .preview-header {
        padding: 12px 15px;
    }
    
    .preview-header h3 {
        font-size: 16px;
    }
    
    .control-btn {
        width: 32px;
        height: 32px;
    }
    
    .preview-info {
        padding: 12px 15px;
        gap: 8px;
    }
    
    .info-label {
        font-size: 11px;
    }
    
    .info-value {
        font-size: 12px;
    }
    
    .recording-progress {
        padding: 15px;
        margin: 20px 10px;
    }
    
    .progress-stats {
        flex-direction: column;
        gap: 10px;
        align-items: flex-start;
    }
    
    .theme-selector {
        padding: 0 10px;
        justify-content: center;
    }
    
    .theme-btn {
        padding: 6px 12px;
        font-size: 14px;
    }
}

/* 超小屏幕设备 */
@media (max-width: 480px) {
    .preview-header {
        flex-direction: column;
        gap: 10px;
        text-align: center;
    }
    
    .preview-controls {
        width: 100%;
        justify-content: center;
    }
    
    .recording-indicator {
        top: 10px;
        left: 10px;
        padding: 6px 10px;
        font-size: 12px;
    }
    
    .recording-dot {
        width: 8px;
        height: 8px;
    }
    
    .progress-header {
        flex-direction: column;
        gap: 5px;
        text-align: center;
    }
    
    .progress-time {
        font-size: 16px;
    }
}
</style>