jquery's position () method

Source: Internet
Author: User

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

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.