How can I add click-to-play to my HTML5 videos without interfering with native controls?

前端 未结 5 612
礼貌的吻别
礼貌的吻别 2021-01-04 01:49

I\'m using the following code to add click-to-play functionality to HTML5 video:

$(\'video\').click(function() {
  if ($(this).get(0).paused) {
    $(this).g         


        
5条回答
  •  时光说笑
    2021-01-04 02:12

    You could try event.stopPropagation and see if that works. Though I think that would either stop the native controls from working, or just not do anything.

    $('video').click(function(event) {
      event.stopPropagation();
      if ($(this).get(0).paused) {
        $(this).get(0).play();
      }
      else {
        $(this).get(0).pause();
      }
    });
    

    If the browser considers the native controls and video all part of the same element (and I believe they do) you're probably out of luck. jQuery's event.target wouldn't allow you to tell the difference between a click on the video and a click on the controls.

    So I think your best option is to build your own controls (old tutorial, still pretty straight forward). Or ask the browser devs to make a click on the video play/pause when controls are enabled. Seems like it should do that by default.

提交回复
热议问题