Definition and usage of the position () method:
This method gets the offset of the matched element relative to some elements.
The returned object contains two integer attributes (top and left) of the object.
This method is valid only for visible elements.
Syntax structure:
$ (selector). Position ()
At the beginning of the tutorial it is said to get the offset of the matching element relative to some elements. Many tutorials say that the offset returned by the method is relative to the parent element, which is not exactly the case, and this method handles the matching element in absolute positioning, but not really setting the matching element to absolute positioning. The offset reference principle for the method is as follows:
1. If the parent element is not anchored (the Position property value is relative, absolute, or fixed), then the offset Reference object is the window.
2. If the parent element has a positioning, then the reference object of the offset is the element nearest to its location,
Instance code:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Position () function-Ant tribe</title><styletype= "Text/css">*{margin:0px;padding:0px;}. Father{Background-color:Green;width:200px;Height:200px;padding:50px;Margin-bottom:50px;Margin-left:100px;}. Children{Height:150px;width:150px;Background-color:Red;Line-height:150px;text-align:Center;}</style><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){ $(". Children"). each (function(){ vartext; Text="Left :"+$( This). Position (). Left; Text+="Top:"+$( This). Position (). Top; $( This). text (text); })})</Script></Head><Body><Divclass= "Father"style= "Position:relative"> <Divclass= "Children"></Div></Div><Divclass= "Father"> <Divclass= "Children"></Div></Div></Body></HTML>
In the top combination of the above code, the offset is obtained relative to the parent element because the parent element uses relative positioning. In the bottom combination, the offset Reference object is the window because the parent element is not positioned.
The original address is: http://www.51texiao.cn/jqueryjiaocheng/2015/0519/1935.html
The original address is: http://www.softwhy.com/
jquery's position () method