Javascript CSS modification learning Chapter 1 location search

Source: Internet
Author: User

Offset
The offsettop and offsetleft attributes of all the elements in the browser are very useful. They will give the coordinates of your element relative to the parent element.

This sectionCodeIt will search for offsetparent up, and then add offsettop and offsetleft. In the end, no matter where the offsetparent is, it will give the real coordinates of your element on the screen.

Explanation
This code is very simple. First input the element to be calculated, and then set the variable curleft and curtop to 0.

Copy code The Code is as follows: function findpos (OBJ ){
VaR curleft = curtop = 0;

If the browser supports offsetparent:

Copy code The Code is as follows: if (obj. offsetparent ){

Every time we find a new object, we add its offsettop and offsetleft to curtop and curleft:

Copy code The Code is as follows: do {
Curleft + = obj. offsetleft;
Curtop + = obj. offsettop;

Tip: return the value of '= '.
The following are tips:

Copy code Code:} while (OBJ = obj. offsetparent );

This is not an expression error. I don't want to use '=' to compare OBJ and obj. offsetparent (that's useless, because an element must be different from its parent element ).

So I use '=' to pass the obj. offsetparent value to OBJ. Here I have a detailed explanation of this technique.

Simple return
This loop ends when the element does not have offsetparent. When offsetparent exists, offsetleft will be added to curleft and offsettop will be added to curtop.

When the loop technique is used, we return the coordinates toProgram.

Copy code Code: return [curleft, curtop];}

Http://www.quirksmode.org/js/findpos.html

Reprinted please keep the following information
Author: Beiyu (TW: @ rehawk)

Related Article

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.