Home > Article > Web Front-end > Detailed explanation of jQuery's Position method usage and compatibility
This article mainly shares knowledge about the use and compatibility of the jQuery Position method. The position method obtains the offset of the matching element relative to the parent element. Friends who are interested should take a look, I hope it can help everyone.
1. Position method
jquery api address: http://jquery.cuishifeng.cn/position.html
The position method obtains the offset of the matching element relative to the parent element .
2. Description
2.1 The difference with offset()
.offset() is to obtain the current coordinates of the element relative to the documet
.position( ) method can obtain the offset position of an element relative to its parent element, which is the element's nearest and positioned ancestor element.
2.2 Value calculation
. The size of the border, margins and padding occupied by the element itself is not counted.
. The border and margin of the parent element are not counted, and the padding of the parent element is included.
3. Sample code
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>jQuery position()示例</title> <style> html { line-height: 1.15; } /*父元素--相对定位*/ .parent { position: relative; width: 200px; height: 400px; /*父元素的margin不计算在内*/ margin-top: 10px; /*父元素的border不计算在内*/ border: 1px solid green; /*父元素的padding计算在内*/ padding-top: 10px; } .child-1 { width: 100px; height: 100px; margin: 0 auto; border: 1px solid #2E8DED; } .child-2 { width: 100px; height: 100px; /*子元素的margin不计算在内*/ margin: 10px auto 0; /*子元素的border不计算在内*/ border: 1px solid #2E8DED; /*子元素的padding不计算在内*/ padding: 10px; } </style> </head> <body> <p class="parent"> <p class="child-1"> first child </p> <p class="child-2" id="no-2"> second child </p> </p> <script src=".output/js/jquery-1.12.4.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> $(document).ready(function() { //获取child-2子元素距离父元素的距离 console.log($('#no-2').position().top); }); </script> </body> </html>
4. Note
For attributes such as line-height of text, the default sizes of browsers (chrome, IE, Firefox) are inconsistent, so they are different The browser position() will be inconsistent when calculating the size, so it is necessary to ensure that all browsers have consistent line-height and other attributes.
The sample code is an example without setting line-height. The value calculated by position() is different in different browsers.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>jQuery position()示例</title> <style> html { /*必须给予一致的设置,否则position()计算值不同*/ /*line-height: 1.15;*/ } /*父元素--相对定位*/ .parent { position: relative; width: 200px; height: 400px; /*父元素的margin不计算在内*/ margin-top: 10px; /*父元素的border不计算在内*/ border: 1px solid green; /*父元素的padding计算在内*/ padding-top: 10px; } .child-1 { width: 100px; height: 100px; margin: 0 auto; border: 1px solid #2E8DED; } .child-2 { width: 100px; height: 100px; /*子元素的margin不计算在内*/ margin: 10px auto 0; /*子元素的border不计算在内*/ border: 1px solid #2E8DED; /*子元素的padding不计算在内*/ padding: 10px; } </style> </head> <body> <p class="parent"> 文字文字 <p class="child-1"> first child </p> <p class="child-2" id="no-2"> second child </p> </p> <script src=".output/js/jquery-1.12.4.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> $(document).ready(function() { //获取child-2子元素距离父元素的距离 console.log($('#no-2').position().top); }); </script> </body> </html>
Related recommendations:
CSS positioning and application scenario example analysis
Detailed explanation of CSS position property examples
The above is the detailed content of Detailed explanation of jQuery's Position method usage and compatibility. For more information, please follow other related articles on the PHP Chinese website!