【问题标题】:Html5 Video - Play/Pause video on screen clickHtml5 视频 - 在屏幕上单击播放/暂停视频
【发布时间】:2014-06-03 11:31:16
【问题描述】:

,嗨,

    <!DOCTYPE html> 
<html> 
<body> 

<div style="text-align:center"> 
  <button onclick="playPause()">Play/Pause</button> 
  <button onclick="makeBig()">Big</button>
  <button onclick="makeSmall()">Small</button>
  <button onclick="makeNormal()">Normal</button>
  <br> 
  <video id="video1" width="420">
    <source src="mov_bbb.mp4" type="video/mp4">
    <source src="mov_bbb.ogg" type="video/ogg">
    Your browser does not support HTML5 video.
  </video>
</div> 

<script> 
var myVideo=document.getElementById("video1"); 

function playPause()
{ 
if (myVideo.paused) 
  myVideo.play(); 
else 
  myVideo.pause(); 
} 

function makeBig()
{ 
myVideo.width=560; 
} 

function makeSmall()
{ 
myVideo.width=320; 
} 

function makeNormal()
{ 
myVideo.width=420; 
} 
</script> 

<p>Video courtesy of <a href="http://www.bigbuckbunny.org/" target="_blank">Big Buck Bunny</a>.</p>
</body> 
</html>

我尝试使用来自以下网站的 html5 视频

http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_video_js_prop

如何在视频屏幕点击播放/暂停视频?

我们将不胜感激。

谢谢。

【问题讨论】:

标签: javascript jquery html video screen


【解决方案1】:

只需点击即可调用函数视频

<video id="video1" onClick="playPause();">
...
</video>

【讨论】:

  • 最好在视频对象本身上调用它,否则点击任何其他元素都会停止视频。
【解决方案2】:

最短路径

onclick="this[this.paused ? 'play' : 'pause']()"

正确(但仍然很短)的方式

...考虑到您已经将视频放在变量中,
您通常应该使用事件侦听器,而不是硬编码 onX 属性...
(即使你有回调!)

var myVideo = document.getElementById("video1");
myVideo.addEventListener('click', function(e){
   e.preventDefault();
   this[this.paused ? 'play' : 'pause']();
});

PS:如果您想知道 hack 是如何执行播放/暂停行的 - 它是基于这样一个事实,即在 JavaScript 中,方法/对象函数基本上是该对象的可调用属性,并且在 JavaScript 中也是如此,您可以参考属性直接someObj.someProperty,也可以通过值或变量,如someObj["someProperty"]var prop = "someProperty"; someObj[prop];

...所以长的相当于单线

if (this.paused) {
   this.play();
} else {
   this.pause();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多