getBoundingClientRect() to get the position of the page element
document.documentElement .getBoundingClientRect
This method returns an object to obtain the left, top, right and bottom positions of an element on the page relative to the browser window, which represent the top, left and right of the element respectively. , the offset pixel values of the lower four boundaries relative to the upper left corner of the browser window (note, not the upper left corner of the document area). And this method is no longer IE Only. FF3.0 and Opera9.5 already support this method. It can be said that the efficiency of obtaining the position of page elements has been greatly improved, so obtaining an element on the page is compared with browsing. The offset of the browser window becomes the use of getBoundingClientRect. According to an article, it's awsome, so handsome =. =Because you don’t have to worry about offset, pagex, clientx, etc., etc. In previous versions of Opera and Firefox, it was necessary to loop through to obtain the absolute position of an element on the page.
Code example:
Demo
For convenience, the Demo directly uses absolutely positioned elements
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31