jQuery 如何不断检查元素是否隐藏/可见?
我的网站上有几个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想研究一下:
http://www.west-wind.com/weblog/posts/2008/Sep/12/jQuery-CSS-Property-Monitoring-Plugin-updated
然后您可以做这样的事情:
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:
我想你想考虑使用setInterval()。类似于:
上面的代码将每秒(1000 毫秒)运行一次视频检查。您也可以使用
$(
而不是jQuery(
。videoInterval
变量将允许您使用clearInterval()
如果您出于任何原因需要停止检查“循环”,我相信此代码需要位于您的$(document).ready(function() {...})
块内。 。I think you want to look into using setInterval(). Something like:
The above code will run your video check every second (1000 milliseconds). You can probably also use
$(
instead ofjQuery(
. ThevideoInterval
variable will let you useclearInterval()
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.