Javascript 如何在事件中暂停 HTML5 视频?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4155329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:08:44  来源:igfitidea点击:

How to pause a HTML5 video on an event?

javascripthtml5-video

提问by neoflox

I am building a website with HTML5-<video>in fullscreen background. There are pages in shape of <div>s becoming visible with SWF-container player in them that cause serious CPU performance issues when both media play. So I want to add a simple javascript command that pauses the background video when a project-<div>becomes visible. Would be nice if it resumes again the playback when it gets closed/hidden.

我正在<video>全屏背景下构建一个带有 HTML5- 的网站。<div>SWF 容器播放器中的s形页面变得可见,这会在两种媒体播放时导致严重的 CPU 性能问题。所以我想添加一个简单的 javascript 命令,当项目<div>变得可见时暂停背景视频。如果它在关闭/隐藏时再次恢复播放会很好。

Thanks in advance.

提前致谢。

回答by Evan Mulawski

document.getElementById('myvideotag').pause();

Note that there is no stop() method. In the function that shows/hides your DIV, place the play/pause function there.

请注意,没有 stop() 方法。在显示/隐藏 DIV 的功能中,将播放/暂停功能放在那里。

More resources here: http://www.digitaria.com/blogs/html5-video-skinning-tutorial-part-2-play-buttons

更多资源在这里:http: //www.digitaria.com/blogs/html5-video-skinning-tutorial-part-2-play-buttons

回答by Eugene Sue

My personal favourite is a small onclick handler added to html attribute, like this:

我个人最喜欢的是添加到 html 属性的一个小的 onclick 处理程序,如下所示:

<video onclick="this.paused? this.play() : this.pause()">

回答by Eugene Sue

since there are not traditional events on div hide/show you have to play() and pause() your video element when you call the functions that make your div appear/disappear

由于 div hide/show 上没有传统事件,因此当您调用使您的 div 出现/消失的函数时,您必须播放()和暂停()您的视频元素