当视频播放器不在视野范围内时,将 HTML5 视频设置为暂停
我想用 JavaScript 来做到这一点。我正在使用 Hakim El Hattab 的 Reveal.js 作为基础来构建演示文稿。
Reveal.js 的工作方式是,您正在查看的当前幻灯片具有 .present 类,任何先前的幻灯片都具有 .past 类,而任何尚未进入视图的幻灯片都具有 .future 类。
我希望它自动暂停设置为 .past 或 .future 的幻灯片内的任何视频。
我该怎么办?
提前致谢!
***********< em>*更新******* *******
所以,多亏了我在css-tricks forums 我能够使用 getElementById 让它在单个视频上工作。
下面是我用来添加 .past 和 .future 类并同时暂停视频的 javascript。
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();
}
我现在遇到的问题是如何将其应用于标签名称(即:视频)或可能的类。
I'd like to do this with javascript. I'm building a presentation using Hakim El Hattab's Reveal.js as a foundation.
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.
I would like it to automatically pause any video that is inside a slide set to .past or .future.
How should I go about this?
Thanks in advance!
************UPDATE**************
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在您已经发布了代码,修复起来更加容易:
看看是否有帮助。如果没有,您使用的是现代且符合标准的浏览器吗?
getElementsByClassName()
是一个相对较新的功能。它适用于我的最新版本的 Chrome。Now that you posted your code, it makes it easier to fix:
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.我不知道
setTimeout
在这里是否有效,但您可以尝试以下操作:告诉我这是否有效。
I don't know if
setTimeout
would work here but you could try the following:Tell me if that works.
@petschekr:猜猜这行不通,因为过去未来的类名不仅设置为 videoelements?!
我不知道揭示是如何工作的以及你使用的是什么类型的播放器!但我的方式是这样的:
当前类只有一个文件,右侧(当前幻灯片)
a)定义像onChange函数一样的东西(例如在每个更改页面+按键的按钮上)
b)在你之前触发该函数切换页面
编辑:伪代码让您有一个想法!
@petschekr :guess this won't work, because classname past future is not only set to videoelements?!
i have no idea how reveals work and what kind of player you are using! but my way would be sth like:
there is only one file with the present class, right (current Slide)
a) define sth like a onChange function (for example on every button which changes pages + keypresses)
b) trigger that function before you switch pages
edit: pseudocode to get you an idea!