JavascriptgetBoundingClientRect () code used to obtain the location of page elements [Revised] Page 1/2 _ javascript skills

Source: Internet
Author: User
This method is no longer IEOnly. FF3.0 + and Opera9.5 + support this method. It can be said that the efficiency of obtaining the page element location can be greatly improved, in earlier versions of Opera and Firefox, the absolute position of elements on the page must be obtained through loops. Document.doc umentElement. getBoundingClientRect
Here is the description of MSDN:
Syntax
ORect = object. getBoundingClientRect () Return Value
Returns a TextRectangle object. Each rectangle has four integer properties (top, left, right, and bottom) that represent a coordinate of the rectangle, in pixels.
Remarks
This method retrieves an object that exposes the left, top, right, and bottom coordinates of the union of rectangles relative to the client's upper-left corner. in Microsoft Internet Explorer 5, the window's upper-left is at 2, 2 (pixels) with respect to the true client.
In practice, this method obtains the positions of an element on the left, top, right, and bottom of the page relative to the browser window. It is hard to understand. The following figure shows how to use it.
This method is no longer IE Only. FF3.0 + and Opera9.5 + support this method. It can be said that the efficiency of obtaining the page element location can be greatly improved, in earlier versions of Opera and Firefox, the absolute position of elements on the page must be obtained through loops.

The following code provides a simple example. you can scroll the scroll bar and then click the red area to view the changes of each value.

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.