当视频播放器不在视野范围内时,将 HTML5 视频设置为暂停

发布于 2025-01-07 14:00:54 字数 1171 浏览 0 评论 0原文

我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神经暖 2025-01-14 14:00:54

现在您已经发布了代码,修复起来更加容易:

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。

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();
    }
}

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.

埖埖迣鎅 2025-01-14 14:00:54

我不知道 setTimeout 在这里是否有效,但您可以尝试以下操作:

function PauseVids() {
    var vids = document.getElementsByClassName("past");
    var vids2 = document.getElementsByClassName("future");
    for (var i = 0; i < vids.length; i++) {
        vids[i].pause();
    }
    for (var i = 0; i < vids2.length; i++) {
        vids2[i].pause();
    }
}

// Within onLoad or $(document).ready()
setTimeout("PauseVids()", 1000);

告诉我这是否有效。

I don't know if setTimeout would work here but you could try the following:

function PauseVids() {
    var vids = document.getElementsByClassName("past");
    var vids2 = document.getElementsByClassName("future");
    for (var i = 0; i < vids.length; i++) {
        vids[i].pause();
    }
    for (var i = 0; i < vids2.length; i++) {
        vids2[i].pause();
    }
}

// Within onLoad or $(document).ready()
setTimeout("PauseVids()", 1000);

Tell me if that works.

最初的梦 2025-01-14 14:00:54

@petschekr:猜猜这行不通,因为过去未来的类名不仅设置为 videoelements?!

我不知道揭示是如何工作的以及你使用的是什么类型的播放器!但我的方式是这样的:

当前类只有一个文件,右侧(当前幻灯片)

a)定义像onChange函数一样的东西(例如在每个更改页面+按键的按钮上)

b)在你之前触发该函数切换页面

function onChange(){
       var currentSlice = document.getElementByClassName('present')  //get current html elemen
       var videoObjects = currentSlice.getElementsByTag('video')  //get videoelements here html5 video
       for each videoObj      
            videoObj.pause()

编辑:伪代码让您有一个想法!

@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

function onChange(){
       var currentSlice = document.getElementByClassName('present')  //get current html elemen
       var videoObjects = currentSlice.getElementsByTag('video')  //get videoelements here html5 video
       for each videoObj      
            videoObj.pause()

edit: pseudocode to get you an idea!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文