简体   繁体   English

块相对于父级的位置

[英]Position of the block relative to the parent

I am using jquery and I am trying to find the position of the block relative to the parent: 我正在使用jquery,并且试图找到相对于父级的块的位置:

$("*", document.body).click(function (e) {
    var offset = $(this).position();
    e.stopPropagation();
    $("#result").text(this.tagName + " coords ( " + offset.left + ", " + offset.top + " )");
});

https://jsfiddle.net/gk8z18vg/3/ https://jsfiddle.net/gk8z18vg/3/

How to get the correct value? 如何获得正确的值? Why is offset left 11 为什么会偏移11

You need to add position relative to the parent container so that the value doesn't concern the position from the entire document but just from the parent. 您需要添加相对于父容器的位置,以便该值与整个文档中的位置无关,而与父文档中的位置无关。 The 11 offset looks like the offset of the container + the multiple borders. 11偏移量看起来像容器的偏移量+多个边框。

#co {
 position: relative;
}

Fixes it. 修复它。

Also note that margin are not concerne when talking about position. 另请注意,在谈论头寸时,余量并不重要。

Hope that helps 希望能有所帮助

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

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