获取div / span标签的位置


103

有人可以告诉我未指定a 或元素的topleft位置吗?divspan

即:

<span id='11a' style='top:55px;' onmouseover="GetPos(this);">stuff</span>
<span id='12a' onmouseover="GetPos(this);">stuff</span>

在上面,如果我这样做:

document.getElementById('11a').style.top

55px返回的值。但是,如果我尝试将其用于span“ 12a”,则不会返回任何内容。

我在页面上有一堆div/ spans,我无法为其指定top/ left属性,但是我需要div在该元素的正下方显示一个。

Answers:


100

此函数将告诉您元素相对于页面的x,y位置。基本上,您必须遍历所有元素的父级,并将它们的偏移量加在一起。

function getPos(el) {
    // yay readability
    for (var lx=0, ly=0;
         el != null;
         lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
    return {x: lx,y: ly};
}

但是,如果您只是想要元素相对于其容器的x,y位置,那么您所需要做的就是:

var x = el.offsetLeft, y = el.offsetTop;

要将元素直接放在该元素的下方,您还需要知道其高度。这存储在offsetHeight / offsetWidth属性中。

var yPositionOfNewElement = el.offsetTop + el.offsetHeight + someMargin;

5
不幸的是,当元素在带有边框的表格内时,此方法在Chrome中失败。由于BODY页边距,它在IE6标准模式下也将失败。
GetFree 2012年

1
只是对用户的警告。这是相对于页面而非窗口的。如果用户使用滚动条滚动,它将返回相同的值。小心。
艾萨克·伯林格

谢谢@IsaacBolinger刚注意到它。有人可以告诉我在调整窗口大小或用户滚动时如何更改它的值吗?
DavidFariña16年

@DavidFariña我为此使用了dojo的几何模块“ dom-geometry”。我不知道该怎么做。
艾萨克·伯林格

不推荐使用-对于更有经验的用户而言可能是显而易见的,但是您尚未指定el需要确切传递的内容(即,请提供示例用法)。
马特

183

您可以getBoundingClientRect()在对元素的引用上调用方法。然后,你可以检查topleftright和/或bottom属性...

var offsets = document.getElementById('11a').getBoundingClientRect();
var top = offsets.top;
var left = offsets.left;

如果使用jQuery,则可以使用更简洁的代码...

var offsets = $('#11a').offset();
var top = offsets.top;
var left = offsets.left;

11
getBoundingClientRect()应该使用+1作为答案-这应该是公认的答案!但是,您不需要“现代浏览器”即可工作,请参阅我在此处显示的兼容性列表:stackoverflow.com/questions/1480133/…。在IE 4.0以上版本(!),Chrome 1.0以上版本,FF 3.0以上版本,Safari 4.0以上版本和Opera中都可以正常运行。
Sk8erPeter

25
请注意,getBoundingClientRect()返回的值是相对于视口而不是文档的。为此,您需要类似的东西top = el.getBoundingClientRect().top + window.pageYOffset - el.ownerDocument.documentElement.clientTop
扎克·布鲁姆

1
还要注意的是getBoundingClientRect需要transform考虑在内。
ExpExc '16

如果第一个非绝对定位的孩子的上边距为左或上边距为左,则此方法不正确。当前此页面上没有准确的解决方案,因此,如果有人知道,请发布答案。
柯蒂斯

16

虽然@nickf的答案有效。如果您不喜欢较旧的浏览器,则可以使用此纯Javascript版本。可以在IE9 +和其他版本中使用

var rect = el.getBoundingClientRect();

var position = {
  top: rect.top + window.pageYOffset,
  left: rect.left + window.pageXOffset
};

12

如Alex所述,您可以使用jQuery offset()获取相对于文档流的位置。使用position()作为其相对于父对象的x,y坐标。

编辑:切换document.readywindow.load因为load等待所有元素,所以您得到它们的大小,而不是简单地准备DOM。以我的经验,load会减少Javascript定位元素的错误。

$(window).load(function(){ 
  // Log the position with jQuery
  var position = $('#myDivInQuestion').position();
  console.log('X: ' + position.left + ", Y: " + position.top );
});

9

对于只需要顶部或左侧位置的任何人,只需对@Nickf的可读代码进行一些修改即可解决问题。

function getTopPos(el) {
    for (var topPos = 0;
        el != null;
        topPos += el.offsetTop, el = el.offsetParent);
    return topPos;
}

function getLeftPos(el) {
    for (var leftPos = 0;
        el != null;
        leftPos += el.offsetLeft, el = el.offsetParent);
    return leftPos;
}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.