views:

179

answers:

3

I used to use cumulativeOffset() and getDimensions() to calculate the bounding box of elements, but just realized that cumulativeOffset returns the top left corner oft the start of an element. Meaning: if an inline element wraps, and the next line is more to the left than the start, I get a displaced bounding box.

After researching a bit, I found that I can use getClientRects() to get all rects. I could then go through, and just take the left position of the rect that's most to the left.

I was wondering if there is a better way of doing this... I just didn't find a boundingBox() prototype function. Did I overlook it?

Edit: I also just found out that getClientRects() is not supported by all browser, so this is no solution.

+1  A: 

I don't see a boundingBox() function either, but I wonder if using the same technique (cumulativeOffset() and getDimensions()) on the parent via: getOffsetParent() would do what you want. getOffSetParent():

"Returns element’s closest positioned ancestor. If none is found, the body element is returned."

Which should account for word-wrapping where the second line is further left.

artlung
Well, if I have a div, and multiple spans inside, and just the last span in the second line wraps, thus becoming a two liner (and creating a third line), the parent information doesn't help me, does it?
enyo
Maybe I'm not understanding the issue. It seems like, if the inline spans are within the **parent**, then the parents bounds hold sway even if the `span` ends up odd sized. Do you have a url featuring the HTML and CSS you are using for this?
artlung
Here ist a link where I try to demonstrate the problem:http://test.opentip.org/inlineSpanProblem.phpMy main goal, is in fact, to get the right border of any element. I don't see any other way than getting the left position and adding the width.
enyo
+1  A: 

I've never heard of a way to do this. You could set it position:relative, drop a 1x1 absolutely positioned div into it, position it right:0, get that div's LEFT + WIDTH, and subtract the offset width of the original inline item from that value.

Saying that, total hack, maybe you need to rethink the reason you want to do this!

dfitzhenry
Mh, this is actually a pretty good idea!I'll test it, and will come back as soon as it's done.
enyo
A: 

The solution given by dfitzhenry seems not working in the case of multiline inline elements. Here's my Javascript solution : get your inline element nextSibling, check if it is an inline element, otherwise create a new inline element, add it to your inline element's parent and then get its offsetLeft:

 **var parentContainer = inline_elm.parentNode;
 var nextsib = inline_elm.nextSibling;
 var remove_next_sibling = false;
 if(!nextsib || nextsib.clientWidth != 0){
     remove_next_sibling = true;
     var temp= document.createElement('span');
     parentContainer.insertBefore(temp, nextsib);
     nextsib = temp;
 }

 var inline_bounding_right = nextsib.offsetLeft;
 if(remove_next_sibling)    parentContainer.removeChild(nextsib);**
Yacine BOURADA