如何使用JavaScript检查元素是否真实可见? [英] How do I check if an element is really visible with JavaScript?

查看:85
本文介绍了如何使用JavaScript检查元素是否真实可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在JavaScript中,您如何检查元素是否实际可见?我不仅仅意味着检查可见性显示属性。我的意思是,检查元素不是




  • 可见性:隐藏 display:none

  • 另一个元素下方

  • 滚动屏幕边缘



出于技术原因,我不能包含任何脚本。然而,我可以使用原型,因为它已经在页面上。

解决方案

对于点2。



我看到没有人建议使用 document.elementFromPoint(x,y),对我来说,测试元素是否嵌套或隐藏的最快方法。您可以将目标元素的偏移量传递给函数。



以下是 elementFromPoint


In JavaScript, how would you check if an element is actually visible?

I don't just mean checking the visibility and display attributes. I mean, checking that the element is not

  • visibility: hidden or display: none
  • underneath another element
  • scrolled off the edge of the screen

For technical reasons I can't include any scripts. I can however use Prototype as it is on the page already.

解决方案

For the point 2.

I see that no one has suggested to use document.elementFromPoint(x,y), to me it is the fastest way to test if an element is nested or hidden by another. You can pass the offsets of the targetted element to the function.

Here's PPK test page on elementFromPoint.

这篇关于如何使用JavaScript检查元素是否真实可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆