JavaScript Learning Location Acquisition

Source: Internet
Author: User

First, get the browser size and location

See also blog: http://www.cnblogs.com/bobodeboke/p/4653920.html

Second, get the size and position of the element

Method one, using the Offsetxxx property

1) Offsetheight: The element occupies space vertically, including the inner margin, and the upper and lower border.

2) Offsetwidth: Elements occupy space horizontally, including inner margin, left and right border.

Note: If you do not want to include the border department, you can use clientwidth and clientheight;

3) Offsetleft: The pixel distance between the left bounding box of the element and the left inner border of the containing element (in the offsetparent reference).

4) OffsetTop: The pixel distance between the Sisu border of the element and the inside border of the containing element (offsetparent reference).

Depending on offsetleft and offsettop, you can get the offset of the element on the page by backtracking up to the root element:

//gets the left offset of the element on the pagefunction Getelementleft (element) {varacturalleft=Element.offsetleft; varCurrent=element.offsetparent;  while(current!=NULL) {Acturalleft+=Current.offsetleft; Current=current.offsetparent; }    returnActuralleft;}//gets the offset of the element on the pagefunction Getelementtop (element) {varacturaltop=Element.offsettop; varCurrent=element.offsetparent;  while(current!=NULL) {Acturaltop+=Current.offsettop; Current=current.offsetparent; }    returnacturaltop;}

Method two, using Getboundingclientrect () method

The method returns a rectangular object that contains four properties of left,top,right and bottom. Where the difference between right and left is equal to the value of offsetwidth, the bottom and top difference and the value of offsetheight are equal. The left and top properties are roughly equal to the front getelementtop and Getelementleft

(ie thinks the upper-left corner of the document is (2,2), others including IE9 think the upper-left corner is (0,0))

Three, the determination of the size and position of the rolling element

1) ScrollHeight: If there is no scrollbar, the total height of the element (also included in the height of the rolling collapse department)

2) ScrollWidth: If there is no scroll pick, the total width of the element

3) ScrollLeft: The number of pixels hidden in the left-hand area by the scroll bar. You can change the scrolling position of an element by setting this property

4) ScrollTop: The number of pixels hidden by the scrollbar in the upper side area. You can change the scrolling position of an element by setting this property

JavaScript Learning Location Acquisition

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.