自定义样式
本章将详细介绍如何自定义videojs-record的界面样式和外观,包括按钮样式、进度条、录制指示器等。
12.1 CSS类名和选择器
videojs-record使用特定的CSS类名来标识各种UI元素:
类名 | 描述 | 用途 |
---|---|---|
.vjs-record-button | 录制按钮 | 开始/停止录制的主要按钮 |
.vjs-pause-button | 暂停按钮 | 暂停/恢复录制的按钮 |
.vjs-record-indicator | 录制指示器 | 显示录制状态的视觉指示器 |
.vjs-record-time | 录制时间显示 | 显示当前录制时间的元素 |
.vjs-record-progress | 录制进度条 | 显示录制进度的进度条 |
.vjs-camera-button | 摄像头切换按钮 | 切换前后摄像头的按钮 |
12.2 基本样式自定义
通过CSS自定义基本的UI元素样式:
基本样式自定义示例
/* 自定义录制按钮样式 */
.vjs-record-button {
background-color: #00758F !important;
border: none !important;
border-radius: 6px !important;
color: white !important;
padding: 10px 20px !important;
font-size: 16px !important;
font-weight: bold !important;
cursor: pointer !important;
transition: all 0.3s ease !important;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2) !important;
}
.vjs-record-button:hover {
background-color: #006073 !important;
transform: translateY(-2px) !important;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3) !important;
}
.vjs-record-button:active {
transform: translateY(0) !important;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2) !important;
}
/* 自定义暂停按钮样式 */
.vjs-pause-button {
background-color: #ffc107 !important;
border: none !important;
border-radius: 6px !important;
color: #212529 !important;
padding: 10px 20px !important;
font-size: 16px !important;
font-weight: bold !important;
cursor: pointer !important;
margin-left: 10px !important;
transition: all 0.3s ease !important;
}
.vjs-pause-button:hover {
background-color: #e0a800 !important;
transform: translateY(-2px) !important;
}
/* 自定义录制指示器 */
.vjs-record-indicator {
width: 16px !important;
height: 16px !important;
background-color: #dc3545 !important;
border-radius: 50% !important;
animation: blink 1s infinite !important;
margin-right: 10px !important;
}
@keyframes blink {
0% { opacity: 1; }
50% { opacity: 0.3; }
100% { opacity: 1; }
}
/* 自定义录制时间显示 */
.vjs-record-time {
color: #00758F !important;
font-weight: bold !important;
font-size: 18px !important;
margin-left: 15px !important;
font-family: 'Courier New', monospace !important;
}
/* 自定义进度条 */
.vjs-record-progress {
height: 8px !important;
background-color: #e9ecef !important;
border-radius: 4px !important;
overflow: hidden !important;
margin: 10px 0 !important;
}
.vjs-record-progress .vjs-record-progress-bar {
height: 100% !important;
background-color: #00758F !important;
transition: width 0.3s ease !important;
}
/* 自定义摄像头切换按钮 */
.vjs-camera-button {
background-color: #28a745 !important;
border: none !important;
border-radius: 6px !important;
color: white !important;
padding: 8px 16px !important;
font-size: 14px !important;
cursor: pointer !important;
margin-left: 10px !important;
transition: all 0.3s ease !important;
}
.vjs-camera-button:hover {
background-color: #218838 !important;
transform: translateY(-1px) !important;
}
12.3 主题定制
创建不同的主题样式:
主题定制示例
/* 深色主题 */
.dark-theme .video-js {
background-color: #1a1a1a !important;
}
.dark-theme .vjs-record-button {
background-color: #00758F !important;
color: white !important;
border: 1px solid #006073 !important;
}
.dark-theme .vjs-pause-button {
background-color: #ffc107 !important;
color: #212529 !important;
border: 1px solid #e0a800 !important;
}
.dark-theme .vjs-record-time {
color: #ffffff !important;
}
.dark-theme .vjs-record-progress {
background-color: #333333 !important;
}
.dark-theme .vjs-record-progress-bar {
background-color: #00758F !important;
}
/* 浅色主题 */
.light-theme .video-js {
background-color: #ffffff !important;
}
.light-theme .vjs-record-button {
background-color: #00758F !important;
color: white !important;
border: 1px solid #006073 !important;
}
.light-theme .vjs-pause-button {
background-color: #ffc107 !important;
color: #212529 !important;
border: 1px solid #e0a800 !important;
}
.light-theme .vjs-record-time {
color: #333333 !important;
}
.light-theme .vjs-record-progress {
background-color: #e9ecef !important;
}
.light-theme .vjs-record-progress-bar {
background-color: #00758F !important;
}
/* 彩色主题 */
.colorful-theme .vjs-record-button {
background: linear-gradient(45deg, #00758F, #00bcd4) !important;
color: white !important;
border: none !important;
border-radius: 20px !important;
}
.colorful-theme .vjs-pause-button {
background: linear-gradient(45deg, #ffc107, #ff9800) !important;
color: #212529 !important;
border: none !important;
border-radius: 20px !important;
}
.colorful-theme .vjs-record-indicator {
background: linear-gradient(45deg, #dc3545, #ff6b6b) !important;
width: 18px !important;
height: 18px !important;
}
/* 圆形按钮主题 */
.circle-theme .vjs-record-button {
width: 50px !important;
height: 50px !important;
border-radius: 50% !important;
padding: 0 !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
background-color: #dc3545 !important;
}
.circle-theme .vjs-pause-button {
width: 50px !important;
height: 50px !important;
border-radius: 50% !important;
padding: 0 !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
background-color: #ffc107 !important;
margin-left: 15px !important;
}
.circle-theme .vjs-record-time {
font-size: 20px !important;
margin-left: 20px !important;
}
12.4 响应式样式
为不同设备屏幕尺寸定制样式:
响应式样式示例
/* 默认样式(桌面端) */
.vjs-record-button {
padding: 12px 24px !important;
font-size: 16px !important;
min-width: 120px !important;
}
.vjs-pause-button {
padding: 12px 24px !important;
font-size: 16px !important;
min-width: 120px !important;
}
.vjs-record-time {
font-size: 18px !important;
}
/* 平板设备样式 */
@media (max-width: 1024px) {
.vjs-record-button {
padding: 10px 20px !important;
font-size: 15px !important;
min-width: 100px !important;
}
.vjs-pause-button {
padding: 10px 20px !important;
font-size: 15px !important;
min-width: 100px !important;
margin-left: 8px !important;
}
.vjs-record-time {
font-size: 16px !important;
margin-left: 12px !important;
}
.vjs-record-indicator {
width: 14px !important;
height: 14px !important;
margin-right: 8px !important;
}
}
/* 手机设备样式 */
@media (max-width: 768px) {
.vjs-record-button {
padding: 12px 20px !important;
font-size: 16px !important;
min-width: 0 !important;
width: 100% !important;
margin-bottom: 10px !important;
}
.vjs-pause-button {
padding: 12px 20px !important;
font-size: 16px !important;
min-width: 0 !important;
width: 100% !important;
margin-left: 0 !important;
margin-bottom: 10px !important;
}
.video-js .vjs-control-bar {
flex-direction: column !important;
align-items: stretch !important;
padding: 10px !important;
}
.vjs-record-time {
font-size: 18px !important;
text-align: center !important;
margin: 10px 0 !important;
}
.vjs-record-indicator {
width: 16px !important;
height: 16px !important;
margin: 0 auto !important;
display: block !important;
}
.vjs-record-progress {
margin: 15px 0 !important;
}
}
/* 小屏幕手机样式 */
@media (max-width: 480px) {
.vjs-record-button {
padding: 14px !important;
font-size: 14px !important;
}
.vjs-pause-button {
padding: 14px !important;
font-size: 14px !important;
}
.vjs-record-time {
font-size: 16px !important;
}
.vjs-record-indicator {
width: 12px !important;
height: 12px !important;
}
/* 隐藏部分非关键元素 */
.vjs-camera-button {
display: none !important;
}
}
12.5 动画效果
添加动画效果增强用户体验:
动画效果示例
/* 录制按钮动画 */
.vjs-record-button {
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
transform: scale(1) !important;
}
.vjs-record-button:hover {
transform: scale(1.05) !important;
}
.vjs-record-button:active {
transform: scale(0.95) !important;
}
/* 录制指示器动画 */
.vjs-record-indicator {
animation: pulse 2s infinite !important;
}
@keyframes pulse {
0% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.7);
}
70% {
transform: scale(1.1);
box-shadow: 0 0 0 10px rgba(220, 53, 69, 0);
}
100% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(220, 53, 69, 0);
}
}
/* 进度条动画 */
.vjs-record-progress-bar {
transition: width 0.5s ease-in-out !important;
}
/* 按钮点击动画 */
@keyframes buttonClick {
0% { transform: scale(1); }
50% { transform: scale(0.9); }
100% { transform: scale(1); }
}
.vjs-record-button.clicked {
animation: buttonClick 0.3s ease !important;
}
/* 录制完成动画 */
@keyframes recordComplete {
0% { background-color: #00758F; }
50% { background-color: #28a745; }
100% { background-color: #00758F; }
}
.vjs-record-button.complete {
animation: recordComplete 1s ease !important;
}
/* 加载动画 */
.vjs-loading-spinner {
border: 4px solid #f3f3f3 !important;
border-top: 4px solid #00758F !important;
border-radius: 50% !important;
width: 30px !important;
height: 30px !important;
animation: spin 1s linear infinite !important;
margin: 0 auto !important;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* 淡入淡出动画 */
.fade-in {
animation: fadeIn 0.5s ease-in !important;
}
.fade-out {
animation: fadeOut 0.5s ease-out !important;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes fadeOut {
from { opacity: 1; transform: translateY(0); }
to { opacity: 0; transform: translateY(-20px); }
}
12.6 自定义控件
创建自定义控件和UI元素:
自定义控件示例
/* 自定义质量选择器 */
.vjs-quality-selector {
display: flex !important;
align-items: center !important;
margin-left: 15px !important;
}
.vjs-quality-selector label {
color: #ffffff !important;
font-size: 14px !important;
margin-right: 8px !important;
}
.vjs-quality-selector select {
background-color: rgba(255, 255, 255, 0.2) !important;
color: white !important;
border: 1px solid rgba(255, 255, 255, 0.3) !important;
border-radius: 4px !important;
padding: 5px 10px !important;
font-size: 14px !important;
}
.vjs-quality-selector select:focus {
outline: none !important;
border-color: #00758F !important;
}
/* 自定义设备选择器 */
.vjs-device-selector {
display: flex !important;
flex-direction: column !important;
margin: 10px !important;
padding: 15px !important;
background-color: rgba(0, 0, 0, 0.7) !important;
border-radius: 8px !important;
}
.vjs-device-selector h4 {
color: white !important;
margin: 0 0 10px 0 !important;
font-size: 16px !important;
}
.vjs-device-selector select {
width: 100% !important;
padding: 8px !important;
margin-bottom: 10px !important;
border-radius: 4px !important;
border: 1px solid #ccc !important;
background-color: white !important;
}
/* 自定义预览窗口 */
.vjs-preview-window {
position: absolute !important;
top: 10px !important;
right: 10px !important;
width: 150px !important;
height: 100px !important;
border: 2px solid #00758F !important;
border-radius: 4px !important;
background-color: #000 !important;
z-index: 1000 !important;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3) !important;
}
.vjs-preview-window video {
width: 100% !important;
height: 100% !important;
object-fit: cover !important;
}
/* 自定义录制统计 */
.vjs-recording-stats {
position: absolute !important;
top: 10px !important;
left: 10px !important;
background-color: rgba(0, 0, 0, 0.7) !important;
color: white !important;
padding: 10px !important;
border-radius: 4px !important;
font-size: 12px !important;
z-index: 1000 !important;
}
.vjs-recording-stats div {
margin-bottom: 5px !important;
}
.vjs-recording-stats .stat-label {
display: inline-block !important;
width: 80px !important;
color: #ccc !important;
}
.vjs-recording-stats .stat-value {
font-weight: bold !important;
color: #00758F !important;
}
提示:在自定义样式时,使用!important可以确保样式优先级,但应谨慎使用,避免过度依赖。