jQuery 如何不断检查元素是否隐藏/可见?

发布于 2024-12-24 20:46:02 字数 342 浏览 6 评论 0原文

我的网站上有几个 HTML5 视频(在滑块内),它们每 x 秒自动循环一次(或当用户单击“下一张幻灯片”时)。

我想停止用户实际上不可见的视频,有什么想法可以实现这一点吗?

我试图做类似的事情,但我猜缺少“每个”,并且它在点击后一直有效(好吧,事实上它不起作用,因为我猜这里“this”使用错误,但你得到重点是,抱歉,我根本不是 JS 人 :():

document.on('click', ".videojs:hidden", function(){
   alert('video hidden!');    
   jQuery(this).player.pause();
});

I have a couple of HTML5 videos on my website (within a slider), they automatically cycle every x seconds (or when user clicks "next slide").

I want to stop the videos that are actually invisible to user, any ideas how to achieve that?

I was tryng to do something like that, but I guess there's "each" missing and it works after click instead all the time (ok, in fact it doesn't work because "this" is used wrong here I guess, but you get the point, sorry, I'm not a JS-guy at all :():

document.on('click', ".videojs:hidden", function(){
   alert('video hidden!');    
   jQuery(this).player.pause();
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海似她心 2024-12-31 20:46:02

您可能想研究一下:

http://www.west-wind.com/weblog/posts/2008/Sep/12/jQuery-CSS-Property-Monitoring-Plugin-updated

然后您可以做这样的事情:

jQuery(".videojs").watch("display,visibility", function() { 
  if(!jQuery(".videojs").is(':visible'))
  {
    alert('video hidden!');    
    jQuery(".videojs").player.pause();
  }
});

You might want to look into this:

http://www.west-wind.com/weblog/posts/2008/Sep/12/jQuery-CSS-Property-Monitoring-Plugin-updated

You can then do something like this:

jQuery(".videojs").watch("display,visibility", function() { 
  if(!jQuery(".videojs").is(':visible'))
  {
    alert('video hidden!');    
    jQuery(".videojs").player.pause();
  }
});
无声无音无过去 2024-12-31 20:46:02

我想你想考虑使用setInterval()。类似于:

var videoInterval = setInterval(function() {
  // video check logic here
}, 1000);

上面的代码将每秒(1000 毫秒)运行一次视频检查。您也可以使用 $( 而不是 jQuery(videoInterval 变量将允许您使用 clearInterval()如果您出于任何原因需要停止检查“循环”,我相信此代码需要位于您的 $(document).ready(function() {...}) 块内。 。

I think you want to look into using setInterval(). Something like:

var videoInterval = setInterval(function() {
  // video check logic here
}, 1000);

The above code will run your video check every second (1000 milliseconds). You can probably also use $( instead of jQuery(. The videoInterval variable will let you use clearInterval() if you need to stop the "loop" of checks for any reason. I believe this code will need to be inside of your $(document).ready(function() {...}) block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文