有没有办法检测 HTML 元素何时从视图中隐藏?
使用 Javascript,是否可以检测某个元素何时不再可见,例如当用户向下滚动足够远或浏览器最小化或被另一个窗口覆盖时? 总体目标是仅当当前广告对用户不可见时才替换广告。
一种想法是让一个非常简单、不可见的 Java Applet 在每次调用 Paint() 方法时与页面进行通信。 我很确定这会起作用,但我想如果可能的话避免使用小程序。
Using Javascript, is it possible to detect when a certain element is no longer visible, such as when the user scrolls down far enough or when the browser is minimized or covered with another window? The overall goal is to swap out an advertisement only when the current ad is not visible to the user.
One idea would be to have a very simple, invisible Java Applet communicate with the page every time the paint() method is called. I'm pretty sure this would work but I'd like to avoid using applets if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定是否有办法检测窗口是否位于元素上方或者窗口是否最小化(尽管我认为您可以通过将某些东西挂接到窗口的模糊中来完成后者?我不确定。 ..)。 无论如何,就滚动而言,这部分问题之前已被问过几次, 是我想出的快速演示,用于展示如何做到这一点。 在示例中,当元素滚动到视图中时会发生一些事情,但逻辑当然是相同的。 不管怎样,代码是:
没有太多 jQuery 特定的内容,所以你可以把这些东西拿出来:
I am not sure if there is a way to detect if a window is over the element or if the window is minimized (although I think you may be able to do the latter by hooking something into the window's blur? I'm not sure...). Anyhow, as far as scrolling, that part of the question has been asked a few times before, and this is the quick demo I came up with to show how to do it. In the example something happens when the element is scrolled into view, but the logic is of course the same. Anyhow, the code is:
There's not much jQuery-specific about it so you can take that stuff out: