2013-04-18 90 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);