我如何使整个视频可点击



使用一个基本的视频,我如何使它播放/暂停当我点击视频,而不是控制?

您可以为整个视频添加一个点击处理程序。

<video id="my-video" controls src="myVideo.mp4"></video>
<script>    
   document.getElementById('my-video').addEventListener('click', function() {
       if (this.paused) {
          this.play();
       } else {
          this.pause();
       }
    });
</script>
<video id="video1" onClick="playPause();">

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

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

详细信息在代码片段的注释

// Reference #lid and #vid
var lid = document.getElementById('lid1');
var vid = document.getElementById('vid1');
// Listen for user to click #lid then...
lid.addEventListener('click', function(event) {
  // ...if #vid1 is paused, play video...
  if (vid.paused) {
    vid.play();
    // ...else pause video
  } else {
    vid.pause();
  }
  /* The only element that needs to know click
  | events is #iid, so stop the event chain from
  | continuing.
  */
  event.stopPropagation();
}, false);
.box {
  position: relative;
  min-width: 300px;
  min-height: 170px;
}
.lid {
  position: absolute;
  width: 100%;
  /* This measurement is just enough for #lid1 
  | to cover #vid1 and keep the controls exposed
  */
  min-height: calc(100% - 32px);
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 32px;
  cursor: pointer;
}
.vid {
  position: absolute;
  height: auto;
  top: 0;
  left: 0;
}
<!--Parent positioned relative-->
<figure id='box1' class='box'>
  <!--#lid1 and #vid1 are positioned absolute-->
  <div id='lid1' class='lid'>
    <!--#vid1 is z-index:0 and #lid1 is z-index:1-->
    <video id="vid1" class="vid" src="http://html5demos.com/assets/dizzy.mp4" controls width='100%'></video>
  </div>
</figure>

相关内容

  • 没有找到相关文章

最新更新