|
下面這是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.
還是實(shí)際解釋下,該方法獲得頁(yè)面中某個(gè)元素的左,上,右和下分別相對(duì)瀏覽器視窗的位置。也不好理解,下面用圖說(shuō)明下。
該方法已經(jīng)不再是IE Only了,F(xiàn)F3.0+和Opera9.5+已經(jīng)支持了該方法,可以說(shuō)在獲得頁(yè)面元素位置上效率能有很大的提高,在以前版本的Opera和Firefox中必須通過(guò)循環(huán)來(lái)獲得元素在頁(yè)面中的絕對(duì)位置。
下面的代碼舉了個(gè)簡(jiǎn)單的例子,可以滾動(dòng)滾動(dòng)條之后點(diǎn)紅色區(qū)域看各個(gè)值的變化。
[Ctrl+A 全選 注:如需引入外部Js需刷新才能執(zhí)行]
有了這個(gè)方法,獲取頁(yè)面元素的位置就簡(jiǎn)單多了,
var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft;
var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;
JavaScript技術(shù):javascript getBoundingClientRect() 來(lái)獲取頁(yè)面元素的位置的代碼[修正版]第1/2頁(yè),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。