当视频播放器不在视野范围内时,将HTML5视频设置为PAUSE [英] Set an HTML5 Video to PAUSE when video player is out of view
问题描述
我想用javascript做这件事。我正在使用Hakim El Hattab的 Reveal.js 作为基础进行演示。
I'd like to do this with javascript. I'm building a presentation using Hakim El Hattab's Reveal.js as a foundation.
Reveal.js的工作方式是你正在查看的当前幻灯片有一个.present类,任何以前的幻灯片都有一个.past类,还有任何幻灯片还没有进入视图有一类.future。
The way Reveal.js works is the current slide you are viewing has a class of .present, any previous slides have a class of .past, and any slides yet to come into view have a class of .future.
我希望它能自动暂停幻灯片组中的任何视频到.past或.future。
I would like it to automatically pause any video that is inside a slide set to .past or .future.
我应该怎么做?
提前致谢!
** * ** * ** * ** * 强> 更新的 * ** * ** * ** * ****
************UPDATE**************
所以感谢我接手了一些方向 css-tricks论坛我能够使用getElementById使用单个视频。
so thanks to some direction i got over on the css-tricks forums i was able to get it working on a single video using getElementById.
下面是我用来添加.past和.future类并同时暂停视频的javascript。
below is the javascript i'm using to add the .past and .future classes and simultaneously pause a video.
if( i < index ) {
// Any element previous to index is given the 'past' class
slide.setAttribute('class', 'past');
document.getElementById('vid').pause();
}
else if( i > index ) {
// Any element subsequent to index is given the 'future' class
slide.setAttribute('class', 'future');
document.getElementById('vid').pause();
}
我现在遇到的问题是如何将其应用于标签名称(即:视频)或可能是一个类。
the issue that i'm having now is how would i apply it to a tag name (ie: video) or possibly a class.
推荐答案
现在你发布了代码,它更容易修复:
Now that you posted your code, it makes it easier to fix:
if( i < index ) {
// Any element previous to index is given the 'past' class
slide.setAttribute('class', 'past');
var vids = document.getElementsByClassName("past");
for (var i = 0; i < vids.length; i++) {
vids[i].pause();
}
}
else if( i > index ) {
// Any element subsequent to index is given the 'future' class
slide.setAttribute('class', 'future');
var vids = document.getElementsByClassName("future");
for (var i = 0; i < vids.length; i++) {
vids[i].pause();
}
}
看看是否有帮助。如果没有,您使用的是符合标准的现代浏览器吗? getElementsByClassName()
是一个相对较新的功能。它适用于我最新版本的Chrome。
See if that helps. If it doesn't, are you using a modern and standards-compliant browser? getElementsByClassName()
is a relatively new feature. It works in the latest version of Chrome for me.
这篇关于当视频播放器不在视野范围内时,将HTML5视频设置为PAUSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!