【问题标题】:jquery <video> .play not working on mobilejquery <video> .play 在移动设备上不起作用
【发布时间】:2017-11-28 10:52:17
【问题描述】:

我的页面上有许多 HTML5 &lt;video&gt; 元素,并且在桌面上运行良好。然而,在 iOS Safari 上,只有长时间按住视频才能播放,而不是正常的点击(应该复制“点击”jquery 功能)。任何人都可以建议为什么视频没有在点击时播放?为什么在短暂持有后会出现这种情况?

$(document).ready(function() {

  // Preload the video
  setTimeout(function() {
    $('.video video')[0].load();
  }, 0);

  $('.video video').click(function() {
    if ($(this).hasClass('is-playing')) {
      $(this).removeClass().addClass('is-paused');
      // Pause video
      $(this)[0].pause();
    } else {
      $(this).removeClass().addClass('is-playing');
      // Play video
      $(this)[0].play();
    }
    return false;
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video src="video-url" poster="poster-url" preload></video>

【问题讨论】:

    标签: jquery ios html video


    【解决方案1】:

    您需要将touchstart 事件与on 函数一起使用:

    $(document).ready(function() {
    
      setTimeout(function() {
        $('.video video')[0].load();
      }, 0);
    
      $('.video video').on('click touchstart', function() {
        // Change is here -------^
        if ($(this).hasClass('is-playing')) {
          $(this).removeClass().addClass('is-paused');
          $(this)[0].pause();
        } else {
          $(this).removeClass().addClass('is-playing');
          $(this)[0].play();
        }
        return false;
      });
    
    });
    

    touchstart

    【讨论】:

    • 非常感谢您的回答。这有所帮助,现在效果更好。但是,它需要 2 次触摸,就好像它需要用户专注于视频才能工作?
    • 欢迎您,不幸的是,这个问题是由OS 引起的,更多关于ios 无所谓,chrome safari 有这个问题。
    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    相关资源
    最近更新 更多