mvm*_*oay 10 firefox html5 internet-explorer css3 html5-video
是否可以在HTML5视频播放器中设置文本轨道(如字幕和字幕)的样式?
我已经找到了Chrome的方法:
video::-webkit-media-text-track-container {
// Style the container
}
video::-webkit-media-text-track-background {
// Style the text background
}
video::-webkit-media-text-track-display {
// Style the text itself
}
Run Code Online (Sandbox Code Playgroud)
这似乎让Safari有点困惑.它可以工作,但渲染是非常错误的.
但更重要的是:如何为Firefox和IE实现这一目标?
小智 8
将此用于Chrome:
video::cue {
// add style here
}
Run Code Online (Sandbox Code Playgroud)
火狐:
尚不支持.打开bug来实现:: cue pseudo-element - https://bugzilla.mozilla.org/show_bug.cgi?id=865395
编辑:
支持FireFox,它的工作方式与Chrome和Opera相似.但Edge或IE尚未提供支持.
迄今为止,我发现的唯一跨浏览器解决方案是:隐藏视频的文本轨道并使用您自己的文本轨道。
这将允许您创建带有类,id等的自己的文本节点,然后可以简单地通过CSS设置样式。
为此,您将利用文本提示的onenter和onexit方法来实现自己的文本节点。
var video = document.querySelector(‘YOUR_VIDEO_SELECTOR’)
tracks = video.textTracks[0],
tracks.mode = 'hidden', // must occur before cues is retrieved
cues = tracks.cues;
var replaceText = function(text) {
$('WHERE_TEXT_GETS_INSERTED').html(text);
},
showText = function() {
$('WHERE_TEXT_GETS_INSERTED').show();
},
hideText = function() {
$('WHERE_TEXT_GETS_INSERTED').hide();
},
cueEnter = function() {
replaceText(this.text);
showText();
},
cueExit = function() {
hideText();
},
videoLoaded = function(e) {
for (var i in cues) {
var cue = cues[i];
cue.onenter = cueEnter;
cue.onexit = cueExit;
}
},
playVideo = function(e) {
video.play();
};
video.addEventListener('loadedmetadata', videoLoaded);
video.addEventLister('load', playVideo);
video.load();
Run Code Online (Sandbox Code Playgroud)
我着手设置字幕的样式,使其具有黑色背景,并位于Safari和Chrome视频的下方。通过以下代码结合使用以下样式编辑.vtt文件,我已经取得了成功。请注意,您必须将样式添加到.vtt文件中,否则在野生动物园中,当视频控件(即使它们被隐藏)出现时,字幕也会跳来跳去:
4
00:00:09.980 --> 00:00:12.640 line:13 position:50% align:middle
size:100%
for just the summer but I ended up staying here.
Run Code Online (Sandbox Code Playgroud)
Chrome和野生动物园字幕的样式:
Chrome使用video :: cue背景色和不透明度。
video::cue {
opacity: 1;
background-color: black;
font-size: 20px !important;
}
Run Code Online (Sandbox Code Playgroud)
Safari使用-webkit-media-text-track-display-backdrop作为背景色。请注意!important会覆盖Safari的固有样式。
video::-webkit-media-text-track-display-backdrop {
background-color: black !important;
overflow: visible !important;
}
Run Code Online (Sandbox Code Playgroud)
以下webkit-media-text-track-display溢出允许在Chrome的标题文本周围进行更多填充:
video::-webkit-media-text-track-display {
overflow: visible !important;
}
Run Code Online (Sandbox Code Playgroud)
可见的溢出对于Safari的以下代码很重要,并且我通过转换设置了视频下方的字幕,该转换依赖于固定的字体大小:
video::-webkit-media-text-track-container {
overflow: visible !important;
transform: translateY(30%) !important;
}
Run Code Online (Sandbox Code Playgroud)
编辑
经过一些调整,我最终将其用于我的项目:
重要提示:从.VTT文件中删除所有内联样式。
确定用户使用的是chrome还是safari。
const rootElement = document.getElementById('root');
const M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
rootElement.className += "web";
if(M[1] === 'chrome'){
rootElement.className += " chrome";
} else {
rootElement.className += " safari";
}
Run Code Online (Sandbox Code Playgroud)
然后在SASS .scss文件中使用以下样式。注意:如果您不使用SASS,则可以简单地为video元素创建一个类并嵌套相应的样式。
.chrome {
video::cue {
font-size: 24px;
opacity: 1;
background-color: black;
-webkit-transform: translateY(10%) !important;
transform: translateY(10%) !important;
}
video::-webkit-media-text-track-display {
overflow: visible !important;
-webkit-box-sizing: border-box;
background: black;
padding: 8px;
border-radius: 16px;
}
video::-webkit-media-text-track-container {
overflow: visible !important;
-webkit-transform: translateY(40%) !important;
transform: translateY(40%) !important;
position: relative;
}
}
.safari {
video::cue {
font-size: 24px;
opacity: 1;
background-color: black;
}
video::-webkit-media-text-track-display-backdrop {
background-color: black !important;
overflow: visible !important;
}
video::-webkit-media-text-track-display {
overflow: visible !important;
-webkit-box-sizing: border-box;
}
video::-webkit-media-text-track-container {
overflow: visible !important;
-webkit-transform: translateY(20%) !important;
transform: translateY(20%) !important;
position: absolute;
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于铬,
video::-webkit-media-text-track-container {
// Style the container
}
video::-webkit-media-text-track-background {
// Style the text background
}
video::-webkit-media-text-track-display {
// Style the text itself
}
Run Code Online (Sandbox Code Playgroud)
您还可以从这些链接中获取一些信息。
| 归档时间: |
|
| 查看次数: |
8946 次 |
| 最近记录: |