确定元素在文档上的绝对位置
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

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];
}

#0

Use jQuery's .offset() function.

#1

As Matt points out, its wise to use a Frameworks function for that, because it is cross-browser compatible and fixes various issues through different versions.

Besides that, i hope you refer to "screen" as the currend shown document in your browser and not the actual computer-screen/display, because that would quite impossible.

推荐文章

如何将vector转换为char*

如何将vector转换为char*

推荐文章

HTTP缓存问题

HTTP缓存问题

推荐文章

删除文件特定日期

删除文件特定日期

推荐文章

MySQL fullsearch问题

MySQL fullsearch问题

推荐文章

在Subversion中更改文件时更新资源路径(CSS/JS)

在Subversion中更改文件时更新资源路径(CSS/JS)

推荐文章

一个jar文件中有多个小程序,一个页面上有多个小程序?

一个jar文件中有多个小程序,一个页面上有多个小程序?

推荐文章

在透视视图中旋转图像

在透视视图中旋转图像

推荐文章

SQL注入工具

SQL注入工具

推荐文章

如何创建自定义事件处理类,如EventArgs

如何创建自定义事件处理类,如EventArgs

推荐文章

php include和closing标记

php include和closing标记

推荐文章

版本控制:最佳的备用程序文件工具,经常单独发布

版本控制:最佳的备用程序文件工具,经常单独发布

推荐文章

WSClient无法在https上连接exchange wsdl

WSClient无法在https上连接exchange wsdl

推荐文章

如何在Cakephp中发出https post请求

如何在Cakephp中发出https post请求

推荐文章

C++中“=0”是什么意思?

C++中“=0”是什么意思?

推荐文章

如何从C/C++中获得VILT大小?

如何从C/C++中获得VILT大小?

推荐文章

Zend框架中的html实体解码等价物

Zend框架中的html实体解码等价物