2012-02-29 73 views
1

我有這個jQuery函數,它獲得我的document的高度並將div粘貼到視圖。jQuery在附加內容時更改.height

它工作正常時,DOM加載時的內容已經在這裏,但如果我追加了一些李來我的容器,我想刷新這個函數來得到新的文件的高度

這裏的js代碼:

$(function() 
{ 
    var documentHeight = 0; 
    var topPadding = 55; 

    var offset = $("#sidebar").offset(); 
    documentHeight = $(document).height(); 
    $(window).scroll(function() { 
     var sideBarHeight = $("#sidebar").height(); 
     if ($(window).scrollTop() > offset.top) 
     { 
      var newPosition = ($(window).scrollTop() - offset.top) + topPadding; 
      var maxPosition = documentHeight - (sideBarHeight + 100); 
      if (newPosition > maxPosition) 
      { 
       newPosition = maxPosition; 
      } 
      $("#sidebar").stop().animate({ 
       marginTop: newPosition 
      }); 
     } 
     else 
     { 
      $("#sidebar").stop().animate({ 
       marginTop: 0 
      }); 
     }; 
    }); 
}); 

和HTML的結構:

<div id="single_content"> 
    <ul> 
     <li>...</li> 
     <li>...</li> 
     <li>...</li> 
     ... 
    <ul> 
</div> 

我想我需要的東西on.change>更新文件的高度在我的函數使用?

感謝您的幫助!

解決:

我通過將該找到了解決辦法:$(document).height();$(window).scroll(function() {});

感謝答案裏面!

+0

難道你不能只修正它在一個變量,並始終使用$(document).height(); ?? – Pluckerpluck 2012-02-29 23:11:58

+0

不,我需要留在這個功能 – user990463 2012-03-01 00:14:32

回答

1
$(window).scroll(function() { 
    documentHeight = $(document).height(); // put this line inside scroll 
    // the rest of code 
}); 
+0

我的第一個猜測我已經刪除。清除不足。 – 2012-03-01 00:59:06