2011-08-25 52 views
0

我得到了一個計算iframe元素高度和文檔高度的javascript。然後使用if來查看iframe元素是打開還是關閉。當它的高度是1px而顯示器是none時。當它的高度是34px並且顯示是阻止時。我用另一個與這個問題無關的函數來完成所有這些。問題是爲什麼當我改變頁面時,如果高度小於上一頁,它不會重新計算高度並將其正確設置,但是如果高度大於上一頁,它會重新計算並相應地設置高度。我在每個頁面的body標籤上使用onload =「javascript:autoHeight」,以便每次加載不同頁面時都會調用該函數,並且只有在完成加載後才能正確獲取高度。如果你需要更多的代碼:http://alomadruga.com.br/temp/home.phpJavaScript高度數學每頁更改recalc

function autoHeight(){ 
var docHeight = $(document).height(); 
var radioHeight = parent.$("#radioiframe").height(); 
if (radioHeight == 34){ 
    parent.$('#content').css({'height': docHeight + 34+ 'px'}); 
    alert("radio iframe is on"); 
} 
else { 
    parent.$('#content').css({'height': docHeight+ 'px'}); 
    alert("radio iframe is off"); 
} 
}; 

感謝每那些幫助,但我設法弄清楚我自己決定我會後的答案,以幫助什麼人被這個問題。我所做的是每次運行函數時,我先將高度設置爲1px,然後讓它運行該函數。

function autoHeight(){ 
    parent.$('#content').css({'height': 1+ 'px'}); 
var docHeight = $(document).height(); 
var radioHeight = parent.$("#radioiframe").height(); 
if (radioHeight == 34){ 
    parent.$('#content').css({'height': docHeight + 34+ 'px'}); 
    alert("radio iframe is on"); 
} 
else { 
    parent.$('#content').css({'height': docHeight+ 'px'}); 
    alert("radio iframe is off"); 
} 
}; 
+0

什麼網頁瀏覽器是你看到的這種行爲?他們全部?他們中有一些? – IcedDante

+0

所有主要瀏覽器 –

+0

這部分代碼看起來很不尋常:'parent。$(「#radioiframe」)。height()'。如果這是jQuery,我無法想象這是在做什麼?你可能是指$(「#radioiframe」)。parent()。height()'? – jfriend00

回答

0

嘗試應用此CSS:

html {height:100%} 

body { 
    margin:0; padding:0; 
    height:auto !important; 
    height:100%; 
    min-height:100%; 
    position:relative; 
} 
+0

謝謝,但這並沒有解決我的問題,因爲JavaScript覆蓋內聯css自動定義高度的CSS。 –

+0

這個CSS給你正確的身體高度,即使然後內容高度<用戶視圖端口高度 – 350D

+0

是啊,但它仍然沒有解決我的問題JavaScript是獲得正確的頁面高度,但當頁面高度較小時之前的頁面高度沒有設置較小的高度,只是在頁腳之後留下了大量的空白 –