首頁  >  文章  >  web前端  >  取得元素距離瀏覽器週邊的位置的方法getBoundingClientRect_javascript技巧

取得元素距離瀏覽器週邊的位置的方法getBoundingClientRect_javascript技巧

WBOY
WBOY原創
2016-05-16 17:36:471408瀏覽
複製程式碼 程式碼如下:

var box = document.getElementById( "g>
var box = document.getElementById( "g>
var box = document.getElementById( "g1" ) alert( box.getBoundingClientRect().right );
alert( box.getBoundingClientRect().bottom );
).left ) */

function getRect( elements ){
var rect = elements.getBoundingClientRect();
var clientTop = document。 documentElement.clientLeft;
return { // 相容ie多出的兩個px
top : rect.top - clientTop, // 距離頂部的位置
bottom : rect.bottom - clientTop, // 距離距離頂部加上元素本身的高度就等於bottom的位置
left : rect.left - clientLeft, // 距離左邊的位置
right : rect.right - clientLeft // 距離右邊的位置就是距離左邊的位置加上元素本身的寬度
};
};
alert( getRect(box).top )
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn