简体   繁体   English

如何获取具有绝对位置的元素的绝对坐标(JavaScript,浏览器)

[英]How to get absolute coordinates of element with absolute position (JavaScript, Browser)

I have some problems with getting coordinates of an element's center which is child of other element with absolute position too.我在获取元素中心的坐标时遇到了一些问题,该中心也是具有绝对位置的其他元素的子元素。 Here you can see a screenshot with the order of elements.在这里,您可以看到带有元素顺序的屏幕截图。

To understand the problem better you can visit my repository at GitHub.为了更好地理解这个问题,你可以访问我在 GitHub 上的存储库。 index.html is in folder "/Resources" index.html 位于文件夹“/Resources”中

So, I have some draggable windows (with interact.js) in some other draggable windows and I want to connect their centers by the lines-divs (they are drawn using div with some transformations).所以,我在其他一些可拖动的窗口中有一些可拖动的窗口(带有interact.js),我想通过lines-div连接它们的中心(它们是使用带有一些转换的div绘制的)。

I use this method to render the lines (maybe there are some problems here).我使用这种方法来渲染线条(可能这里有一些问题)。 I have tried to use jsPlumb for lines drawing, but I failed :(我曾尝试使用 jsPlumb 进行线条绘制,但我失败了 :(

There is getting points x and y.得到点 x 和 y。

// bottom right        
var x1 = offset1.left - margin1.left + size1.width/2 - (this.dom.getAttribute('data-x') || 0);
var y1 = offset1.top - margin1.top + size1.height/2 - (this.dom.getAttribute('data-y') || 0);
// top right
var x2 = offset2.left - margin2.left + size2.width/2 - (this.dom.getAttribute('data-x') || 0);
var y2 = offset2.top - margin2.top + size2.height/2 - (this.dom.getAttribute('data-y') || 0);

(this.dom.getAttribute('data-x') || 0) - that's for Interact.js. (this.dom.getAttribute('data-x') || 0) - 用于 Interact.js。

function getMargins(elem) {
    var top = 0, left = 0;
    while (elem.parentNode) {
        top = top + parseFloat(window.getComputedStyle(elem).marginTop.replace('px', ''));
        left = left + parseFloat(window.getComputedStyle(elem).marginLeft.replace('px', ''));
        elem = elem.parentNode;
    }

    return { top: Math.round(top), left: Math.round(left) }
}

function getOffsetRect(elem) {
    // (1)
    var box = elem.getBoundingClientRect()

    // (2)
    var body = document.body
    var docElem = document.documentElement

    // (3)
    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft

    // (4)
    var clientTop = docElem.clientTop || body.clientTop || 0
    var clientLeft = docElem.clientLeft || body.clientLeft || 0

    // (5)
    var top = box.top + scrollTop - clientTop
    var left = box.left + scrollLeft - clientLeft

    return { top: Math.round(top), left: Math.round(left) }
}

Can you help me with the getting center coordinates?你能帮我获取中心坐标吗? Thanks in advance提前致谢

Try this.. Works best试试这个..效果最好

 function getPos(el) {
     var rect=el.getBoundingClientRect();
     return {x:rect.left,y:rect.top};
 }

Use it like this:像这样使用它:

 var coords = getPos(document.querySelector("el"));
 alert(coords.x);alert(coords.y);

Use jQuery offset() function.使用 jQuery offset() 函数。 It gives you top and left of the element.它为您提供元素的顶部和左侧。
The return top and left are position inside document not window.返回顶部和左侧是文档而不是窗口内的位置。
If you want the position inside window, subtract scrollTop and scrollLeft of window from those values like this:如果您想要窗口内的位置,请从这些值中减去窗口的 scrollTop 和 scrollLeft,如下所示:

var offset = $("selector").offset();
var posY = offset.top - $(window).scrollTop();
var posX = offset.left - $(window).scrollLeft();

Here is the more accurate method这是更准确的方法

I had also experienced similar issue like you are facing...我也遇到过类似的问题,就像你面临的......

Here is the pure javascript solution .这是纯 javascript 解决方案。 Try this尝试这个

  var top = 0,
    left = 0,
    width = 0,
    height = 0;
  let bound = element.getBoundingClientRect();
  height = bound.height;
  width = bound.width;
  do {
    bound = element.getBoundingClientRect();
    top += bound.top;
    left += bound.left;
    element = element.offsetParent;
    if (element !== null) {
      bound = element.getBoundingClientRect();
      top -= bound.top - window.scrollY;
      left -= bound.left - window.scrollX;
    }
  } while (element);
  
  return {
    top: top,
    left: left,
    width: width,
    height: height
  };
};

Then you get your element's coordinates relative to the whole document by calling然后通过调用获取元素相对于整个文档的坐标

left = obj.left;
right = obj.right;
width = obj.width;
height = obj.height;

This method calculates the coordinates including padding , margin , border and scroll offset..该方法计算坐标,包括 padding 、 margin 、border 和滚动偏移量。

if you have draggable object then find its position using event.pageX / Y如果您有可拖动的对象,则使用 event.pageX / Y 找到它的位置

function getOffset(el) {
    var position = el.getBoundingClientRect();
    return {left: position.left + window.scrollX, top: position.top + window.scrollY};
};

var x = getOffset(document.getElementById("MyID")).left;
var y = getOffset(document.getElementById("MyID")).top;

console.log(x, y);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM