简体   繁体   English

如何使用 jQuery 根据可见性播放/暂停视频

[英]How to play/pause video depending on visibility with jQuery

I want one or more videos to play/pause depending on whether or not they're in the viewport.我希望播放/暂停一个或多个视频,具体取决于它们是否在视口中。

I understand this can be achieved with jQuery (and plugins), so I've tried using the isInViewport plugin along with the following code, but I receive the error shown below.我知道这可以通过 jQuery (和插件)来实现,所以我尝试使用isInViewport插件以及以下代码,但我收到如下所示的错误。

HTML HTML

<script src="scripts/jquery.js"></script>
<script src="scripts/isInViewport.js"></script>
[...]
<video id="video1" src="file.mp4" width="50%"></video>
[...]
<script src="scripts/script.js"></script>

JS JS

var $video = document.getElementsByTagName("video1");

if ($video.is(":in-viewport")) {
  $video.play();
} else {
  $video.pause();
};

Console安慰

TypeError: $video.is is not a function

try尝试

$('video').each(function() {
  if ($(this).is(":in-viewport")) {
    $(this)[0].play();
  } else {
    $(this)[0].pause();
  }
}
);

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

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