I have (1) :
a big container :
<div id="container">
which hasabsolute
positionninginside this container, lots of
<div class="txt">
which haveabsolute
positionning as well
I would like to find the coordinates of a bounding box that contains all the <div class="txt">
elements. I have this code :
minX = 1000000, maxX = - 1000000;
minY = 1000000, maxY = - 1000000;
$(".txt").each(function(index) {
minX = Math.min(minX, parseFloat($(this).css("left")));
maxX = Math.max(maxX, parseFloat($(this).css("left")));
minY = Math.min(minY, parseFloat($(this).css("top")));
maxY = Math.max(maxY, parseFloat($(this).css("top")));
});
I don't think it's really correct because maxX
will contain the max of each element's left
, thus the max of the top-left-corner of each element. So the bouding-box will not contain the <div>
which is the most on the left.
Then, in order to take this into account, I could compute
maxX = Math.max(maxX, parseFloat($(this).css("left")) + parseFloat($(this).css("width")))
but it begins to be dirty.
Is there a more elegant way to know the bounding box of lots of elements ?
Notes : (1) : It is for my project named BigPicture, the div name are a little different on the website.
(2) : would this fit more in codereview.SE ?