2013-04-18 133 views
1

我有一个div,假设夹在多个元素之间并位于侧栏上。我需要计算div和它下面的元素之间有多少空间。然后将div的最大高度设置为它的当前高度加上下面的空间,以便它永远不会超过该高度(从而扩展页面)。最初的最大高度,将是一个非常小的值,这应该由JS来改变。 所以,基本上:使用javascript计算div最大高度

<thing></thing> 
<div id="mydiv"></div> 
<another></another> 

,我需要这样的东西(jQuery的例子):

var spacebelow = space between mydiv and <another>  
var daheight = (currentheight + spacebelow); 
    $("#mydiv").attr("style", "max-height:" + daheight + "px"); 

反正是有这样做吗?

回答

0

我想你想是这样的: -

var divoffset=$(#divID).offset(); 
    var divanotheroffset=$(#divanotherID).offset(); 
    var spacebelow =divanotheroffset.top-divoffset.top; 
    var currentheight =$(#divID).height(); 
    var daheight = (currentheight + spacebelow); 
    //set the max-height:- 
    $("#mydiv").attr("max-height",daheight);