jquery position (), offset (), scrolltop ()/scrollleft ()

Source: Internet
Author: User

---restore content starts---

jquery Locator Function: Offset,position,scrolltop/scrollleft

(1) Offset: Gets the offset of the current element relative to the document . Valid only for visible elements.

(2) Position: Gets the relative offset of the grandparent node of the element relative to the nearest position for relative or absolute .

(3) ScrollTop ()/scrollleft () is the distance from the top of the element's scroll bar, respectively.

$ (selector). Offset () and $ (selector). Position () returns the value of the object Top/left that contains the top, left property as number

The return value of ScrollTop ()/scrollleft () also returns the object itself as number type scroll ()

Reference examples

Test code:

<body height="1500px"> <div style="position:relative;margin-top:1000px;height:300px;border:1px solid #0C6;"> <p style="margin:50px;">compute my height</p> </div> </body>

The results obtained in Firebug are as follows:

$(‘div‘).offset() top:1000;left:8;  //浏览器默认body 与视窗margin 为8px $(‘p‘).offset() top:1051;left:9;   $(‘div‘).scrollTop()=0;$(‘div‘).scrollLeft()=0;
$(‘p‘).position();<br>top:0 ;left:0

The result feels strange, and the 12th result is expected, but the third one is the result of pulling the scrollbar to the bottom of the line.

Strange things have come up.

<body style="height:1500px;"> <div style="position:relative;margin-top:1000px;height:300px;"> <p style="padding:50px;">compute my height</p> </div> </body>
$(‘div‘).offset() top:1000;left:8; $(‘p‘).offset() top:1000;left:8; $(‘div‘).scrollTop()=0;$(‘div‘).scrollLeft()=0;
$(‘p‘).position();<br>top:0 ;left:0;
 

Padding not work!!! Box model left to right margin-left |border-left padding-left width padding-right|border-right margin-right

The value read by offset () position () is Margin-left (margin-top) +border-left (border-top) padding not inside

Continue to make changes:

<body style="height:1500px;"> <div style="position:relative;margin-top:1000px;height:300px;border:1px solid #666;"> <p style="padding:50px;">compute my height</p> </div> </body>

$(‘div‘).offset()<br>top:1000;left:8;<br>  $(‘p‘).offset()<br> top:1017;left:9; <br> //div>p会产生8px的margin $(‘div‘).scrollTop()=0;<br>$(‘div‘).scrollLeft()=0;<br> $(‘p‘).position();<br>top:0;left:0;

The inline element will default to 1px intervals

---restore content ends---

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.