[Translated works] JavaScript CSS modification learning Chapter 1: location search

Source: Internet
Author: User
ArticleDirectory
    • Tip: return the value of '= '.
    • Simple return

In this chapter, I will show you how to find the element location on the page.Code. It can help you find the real location. If you change the size of the window and run the code again, it will also tell you the new location.

 

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 code will look up offsetparent, 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.

 
 function findpos (OBJ) {var curleft = curtop = 0; 

If the browser supports offsetparent:

 If (obj. offsetparent) {

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

 do {curleft + = obj. offsetleft; curtop + = obj. offsettop; 

tip: return the value of '='.

the following tips:

}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 using loop technology, we return the coordinates to the program that calls this function .

 return [curleft, curtop];} 

translation address: http://www.quirksmode.org/js/findpos.html

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.