Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 17:36:47
Original
1445 people have browsed it
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 )
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template