document.documentElement.getBoundingClientRect 下面這是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. 還是實際解釋下,該方法獲得頁面中某個元素的左,上,右和下分別相對瀏覽器視窗的位置。也不好理解,下面用圖說明下。 該方法已經不再是IE Only了,FF3.0+和Opera9.5+已經支持了該方法,可以說在獲得頁面元素位置上效率能有很大的提高,在以前版本的Opera和Firefox中必須通過循環來獲得元素在頁面中的絕對位置。
有了這個方法,獲取頁面元素的位置就簡單多了, var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft; var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;