vendredi 17 octobre 2014

Find the bounding box that contains all


Vote count:

0




I have (1) :




  • a big container : <div id="container"> which has absolute positionning




  • inside this container, lots of <div class="txt"> which have absolute positionning as well




enter image description here


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.


Is there a more elegant way to know the bounding box of lots of elements ?




Note (1) : It is for my project named BigPicture, the div name are a little different on the website.


Note (1) : would this fit more in codereview.SE ?



asked 41 secs ago

Basj

864






Find the bounding box that contains all

Aucun commentaire:

Enregistrer un commentaire