Html5视频 - 在屏幕上点击播放/暂停视频

时间:2014-06-03 11:31:17

标签: javascript jquery html5 video screen

,HI,

    <!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

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

任何帮助将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:6)

只需点击

即可调用功能视频
<video id="video1" onClick="playPause();">
...
</video>

答案 1 :(得分:0)

最短的方法

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();
}
相关问题