2012-04-10 69 views
1

我有一個絕對位於頁面右下角的div。問題是,如果div包含太多的內容,那麼內容的頂部將消失在頁面的頂部。我想要做的是將div的最大高度設置爲等於身高減去170px。設置元素高度等於身高減去170px

我已經試過了幾個教程在線,但沒有得到很遠:

什麼Iv'e走到這一步是:

<script> 
var x, y, z; 
y = 170; 
function getDocHeight() { 
    var x = document; 
    return Math.max(
     Math.max(x.body.scrollHeight, x.documentElement.scrollHeight), 
     Math.max(x.body.offsetHeight, x.documentElement.offsetHeight), 
     Math.max(x.body.clientHeight, x.documentElement.clientHeight) 
    ); 
}; 
z = x-y;//document height minus 170. this is the height we need for the info div 
document.write (z); 
document.write (y); 
document.write (x); 
</script> 

什麼上面的輸出是NaN170undefined所以我甚至沒有嘗試設置div id =「info」的高度

所有幫助非常感謝。

肖恩

+6

LMAO,你使用jQuery! '$(「body」)。height() - 170' ... TADA! – SpYk3HH 2012-04-10 15:05:06

+1

'x'是'undefined',因爲你沒有設置它的值。在函數內部,它的值是'document',但外層'x'從來沒有設置過。既然你從不調用'getDocHeight'函數,我假設你打算做'x = getDocHeight();'; – 2012-04-10 15:14:14

+0

...另外,我沒有看到嵌套的'Math.max'調用點。爲什麼不只是一次調用所有的值? – 2012-04-10 15:17:28

回答

1

如果您正在使用JQuery,你可以做到以下幾點:

z = $("body").height() - 170; 
+1

'$('#body')'?我認爲你的意思是$('body')'。 – 2012-04-10 15:13:44

+0

@SKS Ah錯字,謝謝! – mattytommo 2012-04-10 15:14:43

+1

'.height()'返回一個整數。不需要'parseInt'。 – 2012-04-10 15:19:15