确定元素的绝对文档位置 [英] Determining an element's absolute on-document position

查看:78
本文介绍了确定元素的绝对文档位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经有了这样的代码(合计clientWidth和clientHeight ),但在jQuery的滚动效果开始工作时似乎无效。



事实上,我需要检测位置的元素,属于 div ,其中 overflow =hidden用于jQuery滚动效果。



当我尝试使用当前代码获取元素的位置时,我得到一个相对于可滚动面板长度的宽度值。当我滚动面板时,的位置保持不变(因此,我最终需要放置的工具提示放置得太精确,锚定控件隐藏在容器面板溢出后的确切位置。 / p>

我的问题是



如何使当前的代码适应这个需求?

 函数findPos(obj){

var curleft = curtop = 0;
if(obj.offsetParent){
do {
curleft + = obj.offsetLeft;
curtop + = obj.offsetTop;
} while(obj = obj.offsetParent);
}
return [curleft,curtop];
}


解决方案

使用jQuery的 .offset() 函数。 p>

I need to determine the exact on-screen coordinates (relative to document) of an HTML element.

I already have code that does this (summing clientWidth and clientHeight of each parent object up to the root), but seemed to be ineffective when jQuery's scroll effect comes into work.

The elements I need to detect the position of, in fact, are part of a div with overflow="hidden" that is used for jQuery scrolling effect.

When I try to get the element's position with the current code, I get a width value that is relative to the length of the scrollable panel. When I scroll the panel, the position remains the same (so, the tooltip I ultimately need to position is placed too right, in the exact position the anchor control is when hidden behind the container panel's overflow.

My question is

How to adapt the current code to this requirement?

function findPos(obj) {

    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

解决方案

Use jQuery's .offset() function.

这篇关于确定元素的绝对文档位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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