How can I calculate the height of an element cross-browser using jQuery? -
i created plugin bottom-align element. in it's simplest form did this:
- get height of outerelement (div)
- get height of current element
- result = outerheight - height of current element
- sett css attribute 'top' = result.
and works... in firefox , ie8, not in opera or google chrome.
i'm guessing has borders, padding , margin. have in order make work cross-browser?
update
code has been revised , working now.
(function($){ $.fn.alignbottom = function() { var defaults = { outerhight: 0, elementheight: 0 }; var options = $.extend(defaults, options); var bpheight = 0; // border + padding return this.each(function() { options.outerhight = $(this).parent().outerheight(); bpheight = options.outerhight - $(this).parent().height(); options.elementheight = $(this).outerheight(true) + bpheight; $(this).css({'position':'relative','top':options.outerhight-(options.elementheight)+'px'}); }); }; })(jquery);
the html this:
div class="myouterdiv"> <div class="floatleft"> variable content here </div> <img class="bottomalign floatright" src="" /> </div> </div>
and applying jquery plugin:
jquery(".bottomalign").alignbottom();
you need outerheight()
jquery method:
options.outerhight = $(this).parent().outerheight(); options.elementheight = $(this).outerheight( true ); // include margins
you need play .position().top
distance element moved away top of containing element:
var new_top = $(this).parent().outerheight() - $(this).outerheight() - $(this).position().top;
another approach
a couple things position:relative
. elements positioned take space, can shifted in direction. note shifting them not change took space, element displays.
about position:absolute
. absolutely positioned element not take space, needs inside element has position (i.e. position:relative
)
so, in straight css:
.myouterdiv { position: relative } .bottomalign { position: absolute; bottom: 0 }
but note position used take (i.e. because of float right) no longer take up.
Comments
Post a Comment