2013-06-13 34 views
0

我正在檢查window.height和content.height並根據內容的高度給出動態高度。我爲什麼這麼做?因爲當內容較少時我想將頁腳放在底部。有沒有其他的jQuery .ready?

所以我有這個代碼工作正常,有一個例外。

<script type="text/javascript"> 
     jQuery(document).ready(function(){ 
     var s = jQuery(window).height(); 
     var n = jQuery("#content").height(); 
     if(n<s-270&&n>0) 
      jQuery("#content").height(s-270); 
     }); 
</script> 

它不能正常工作時,我將這個頁面http://bmsc.tfei.info/en/staff 這是使用標籤模塊。當我刷新兩次或更多時間後,它就開始工作了。 我想在滿載後檢查一切(窗口,內容)的高度。 有什麼建議嗎?

+0

聽起來像一個圖像緩存問題'$(window).load(function(){});'將在整個頁面完全加載時執行,包括所有的框架,物體和圖像 – Pete

+0

@Pete編輯的評論現在更有意義;) –

回答

2

在這種情況下,使用窗口onload事件:

jQuery(window).on('load',function(){ 
     var s = jQuery(window).height(); 
     var n = jQuery("#content").height(); 
     if(n<s-270&&n>0) 
      jQuery("#content").height(s-270); 
     }); 

當所有異步元素(圖片,腳本)已加載此事件。

而當你是相對於窗口處理函數裏面,你可以寫:

var s = jQuery(this).height(); 
+0

完美答案!!! –

0

您可以使用resize function爲此,

一樣,

<script type="text/javascript"> 

    function test() 
    { 
      var s = jQuery(window).height(); 
      var n = jQuery("#content").height(); 
      if(n<s-270&&n>0) 
       jQuery("#content").height(s-270); 
    } 

    jQuery(document).ready(function(){ 
     test(); 
    }); 
    jQuery('window').on('resize',function(){ 
     test(); 
    }); 
    // you can call load and resize once like, 

    jQuery('window').on('load resize',function(){ 
     test(); 
    }); 
</script> 
+0

@roasted最後的代碼將是一個替代方案,我寫了一個評論。 –

+0

哎呀抱歉,沒有閱讀評論 –

0
jQuery(window).on('load',function() { 
/* code goes here*/ 
}); 
+3

這是什麼添加到烤的答案? – lifetimes