简体   繁体   English

如何动态添加HTML5视频控件

[英]How to Add HTML5 Video controls dynamically

I have a video slider which does not include controls in the 'video' tags. 我有一个视频滑块,该滑块在“视频”标签中不包含控件。 I am using a custom play button to initiate videos. 我正在使用自定义播放按钮来启动视频。 However, once the video has begun playing, I am fading out my custom play button and would like the standard html5 video controls to take over. 然而,一旦视频开始播放时,我淡出我的自定义播放按钮,想标准的HTML5视频控件进行接管。 Is there a way to append the controls to html5 video only if the video is playing. 只有在视频播放时,才可以将控件附加到html5视频中。 Any help is much appreciated. 任何帮助深表感谢。

 $(function(){ $('#btn').on('click', function(){ $('#vid')[0].play(); $(this).hide();}); $('#vid').on('play', function (e) { $(this).attr('controls','true'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <video width="400" id="vid" > <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4"/> Your browser does not support HTML5 video. </video> <button id="btn">Play</button> 

You will want to utilize the 'playing' listener event . 您将要利用“播放”监听器事件

$('video').on('playing', function (e) {
  if (!this.hasAttribute("controls")) {
     this.setAttribute("controls","controls") 
  }
});

When the playing listener fires, you can check and see if the controls have been added. playing侦听器启动时,您可以检查并查看是否已添加控件。 If they don't exist, it will see the native controls attribute. 如果它们不存在,它将看到本机controls属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM