Home >Web Front-end >JS Tutorial >How to get the position of an element from the surroundings of the browser getBoundingClientRect_javascript technique

How to get the position of an element from the surroundings of the browser getBoundingClientRect_javascript technique

WBOY
WBOYOriginal
2016-05-16 17:36:471472browse
Copy code The code is as follows:

var box = document.getElementById( "gaga1" );
/* alert( box.getBoundingClientRect().top );
alert( box.getBoundingClientRect().right );
alert( box.getBoundingClientRect().bottom );
alert( box.getBoundingClientRect( ).left ) */

function getRect( elements ){
var rect = elements.getBoundingClientRect();
var clientTop = document.documentElement.clientTop;
var clientLeft = document. documentElement.clientLeft;
return { // Compatible with the two extra pixels in IE
top : rect.top - clientTop, // Position from the top
bottom : rect.bottom - clientTop, // Distance The top plus the height of the element itself is equal to the position of the bottom
left : rect.left - clientLeft, // The position from the left
right : rect.right - clientLeft // The position from the right is the position from the left Plus the width of the element itself
};
};
alert( getRect(box).top )
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn