如何在 Javascript 中获取对象在页面上的绝对位置?

发布于 2024-08-05 16:10:44 字数 218 浏览 5 评论 0原文

我想在 Javascript 中获取对象在页面上的绝对 x,y 位置。我该怎么做?

我尝试了 obj.offsetTop 和 obj.offsetLeft,但它们仅给出相对于父元素的位置。我想我可以循环并添加父级的偏移量及其父级的偏移量,依此类推,直到到达没有父级的对象,但似乎应该有更好的方法。谷歌搜索并没有找到太多结果,甚至网站搜索也没有找到任何东西。

另外,我无法使用 jQuery。

I want to get an object's absolute x,y position on the page in Javascript. How can I do this?

I tried obj.offsetTop and obj.offsetLeft, but those only give the position relative to the parent element. I guess I could loop through and add the parent's offset, and its parent's offset, and so on until I get to the object with no parent, but it seems like there should be a better way. Googling didn't turn up much, and even SO site search isn't finding anything.

Also, I can't use jQuery.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2024-08-12 16:10:44
var cumulativeOffset = function(element) {
    var top = 0, left = 0;
    do {
        top += element.offsetTop  || 0;
        left += element.offsetLeft || 0;
        element = element.offsetParent;
    } while(element);

    return {
        top: top,
        left: left
    };
};

(无耻地窃取PrototypeJS的方法;更改代码风格、变量名和返回值以保护无辜者)

var cumulativeOffset = function(element) {
    var top = 0, left = 0;
    do {
        top += element.offsetTop  || 0;
        left += element.offsetLeft || 0;
        element = element.offsetParent;
    } while(element);

    return {
        top: top,
        left: left
    };
};

(Method shamelessly stolen from PrototypeJS; code style, variable names and return value changed to protect the innocent)

む无字情书 2024-08-12 16:10:44

我绝对建议使用 element.getBoundingClientRect()

https ://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect

摘要

返回包含一组文本的文本矩形对象
矩形。

语法

var rectObject = object.getBoundingClientRect();

返回

返回的值是一个 TextRectangle 对象,它是这
getClientRects() 用于元素,即 CSS
与元素关联的边框。

返回值是一个TextRectangle对象,其中包含只读
描述边框框的 lefttoprightbottom 属性,位于
像素,左上角相对于视口的左上角。

这是取自链接的 MDN 站点的浏览器兼容性表:

+---------------+--------+-----------------+-------------------+-------+--------+
|    Feature    | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
+---------------+--------+-----------------+-------------------+-------+--------+
| Basic support | 1.0    | 3.0 (1.9)       | 4.0               | (Yes) | 4.0    |
+---------------+--------+-----------------+-------------------+-------+--------+

它受到广泛支持,并且非常易于使用,更不用说它非常快了。以下是 John Resig 的相关文章: http://ejohn.org/blog/getboundingclientrect-is- Awesome/

你可以像这样使用它:

var logo = document.getElementById('hlogo');
var logoTextRectangle = logo.getBoundingClientRect();

console.log("logo's left pos.:", logoTextRectangle.left);
console.log("logo's right pos.:", logoTextRectangle.right);

这是一个非常简单的示例http ://jsbin.com/awisom/2(您可以通过点击右上角的“在 JS Bin 中编辑”来查看和编辑代码)。

或者这是使用 Chrome 控制台的另一个:
使用元素。 Chrome 中的 getBoundingClientRect()

注意:

我必须提到 getBoundingClientRect() 方法的 widthheight 属性返回值在 Internet Explorer 8 中是未定义。但它可以在 Chrome 26.x、Firefox 20.x 和 Opera 12.x 中运行。 IE8 中的解决方法:对于 width,您可以减去返回值的 right 和 left 属性,对于 height,您可以减去 Bottom 和 Top 属性 (像这样)。

I would definitely suggest using element.getBoundingClientRect().

https://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect

Summary

Returns a text rectangle object that encloses a group of text
rectangles.

Syntax

var rectObject = object.getBoundingClientRect();

Returns

The returned value is a TextRectangle object which is the union of the
rectangles returned by getClientRects() for the element, i.e., the CSS
border-boxes associated with the element.

The returned value is a TextRectangle object, which contains read-only
left, top, right and bottom properties describing the border-box, in
pixels, with the top-left relative to the top-left of the viewport.

Here's a browser compatibility table taken from the linked MDN site:

+---------------+--------+-----------------+-------------------+-------+--------+
|    Feature    | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
+---------------+--------+-----------------+-------------------+-------+--------+
| Basic support | 1.0    | 3.0 (1.9)       | 4.0               | (Yes) | 4.0    |
+---------------+--------+-----------------+-------------------+-------+--------+

It's widely supported, and is really easy to use, not to mention that it's really fast. Here's a related article from John Resig: http://ejohn.org/blog/getboundingclientrect-is-awesome/

You can use it like this:

var logo = document.getElementById('hlogo');
var logoTextRectangle = logo.getBoundingClientRect();

console.log("logo's left pos.:", logoTextRectangle.left);
console.log("logo's right pos.:", logoTextRectangle.right);

Here's a really simple example: http://jsbin.com/awisom/2 (you can view and edit the code by clicking "Edit in JS Bin" in the upper right corner).

Or here's another one using Chrome's console:
Using element.getBoundingClientRect() in Chrome

Note:

I have to mention that the width and height attributes of the getBoundingClientRect() method's return value are undefined in Internet Explorer 8. It works in Chrome 26.x, Firefox 20.x and Opera 12.x though. Workaround in IE8: for width, you could subtract the return value's right and left attributes, and for height, you could subtract bottom and top attributes (like this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文