Sunday 14 July 2019

javascript - Get position off all divs




I want get the the position (screen coordinates) of all divs in an iframe using javascript. No jQuery.



I can get all the divs in an array like this:




var arr = window.frames[0].document.getElementsByTagName("div");


Then i tried to do:



var arr = window.frames[0].document.getElementsByTagName("div").x;


But it didn't work. Console logs undefined. Any ideas?



Answer



Try with this:



var divs = window.frames[0].document.getElementsByTagName("div")
i = 0, rects = [];
for (; i < divs.length; i++)
rects.push(divs[i].getBoundingClientRect());


Now the array rects contains objects that define the position relative to the viewport of all your divs. Each of the elements will be something like this:




{
top: ...,
right: ...,
bottom: ...,
left: ...,
width: ...,
height: ...
}



Those properties are the number of pixel from each border (or the width or height).



IE6-8 don't report the width and height properties, though, but it's easy to compute them.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...