---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---