通过javascript获取元素的位置 [英] Get position of element by javascript

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

问题描述

我已经看过十几个脚本可以捕获页面中元素/对象的x和y位置。但是当网页在主体上使用边距或其他元素,绝对/相对元素时,我总是遇到捕捉x和y的问题。

I've seen a dozen of script that can catch the x and y position of an element/object within the page. But i am always having trouble with catching the x and y when the webpage is using margins at the body, or other elements, absolute/relative elements, such like that.

无论使用什么边距或填充,都能提供确切位置的解决方案。

Is there a solution wich provides the exact position, no matter what margins or paddings are used.

:)

推荐答案

我使用以下代码移动div框以跟随此 Web IME网站中的光标

I use following code to move div box to follow cursor in this Web IME site

function xy(x) {
    o = document.getElementById(x);
    var l =o.offsetLeft; var t = o.offsetTop;
    while (o=o.offsetParent)
        l += o.offsetLeft;
    o = document.getElementById(x);
    while (o=o.offsetParent)
        t += o.offsetTop;
    return [l,t];
}

它返回一个数组[left,top],

Its return an array [left,top],

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

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