可能的复制品:
JQuery-在滚动 之后检查元素是否可见
我试图确定一个元素是否在屏幕上可见。为此,我尝试使用 offsetTop 查找元素的垂直位置,但返回的值不正确。在这种情况下,除非向下滚动,否则元素不可见。尽管如此,当屏幕高度为703时,offsetTop 返回一个值618,因此根据 offsetTop,元素应该是可见的。
我使用的代码如下:
function posY(obj)
{
var curtop = 0;
if( obj.offsetParent )
{
while(1)
{
curtop += obj.offsetTop;
if( !obj.offsetParent )
{
break;
}
obj = obj.offsetParent;
}
} else if( obj.y )
{
curtop += obj.y;
}
return curtop;
}
先谢谢你!