2010-12-23 57 views
1

我有一個<div>標記,ID爲page,在ajax調用後div標記填充了'n'個<li>元素。當我滾動到div的末尾(#page)時,我需要添加更多<li>我無法使用以下代碼。可以通過授權完成嗎?jquery中的滾動綁定問題

我的代碼:

$(document).ready(function(){ 
    $("#page").bind("scroll",function(e){ 
      if($(this)[0].scrollHeight - $(this).scrollTop() < $(this).outerHeight()) 
      { 
       //End of scroll ;ajax data places here 
      } 
    }); 
}); 
+0

你試過委託/活? – yoda 2010-12-23 14:16:13

回答

1

你爲什麼不能用這些代碼做呢?據我瞭解,你想綁定到滾動。我看到的可能是一個問題,就是你的div上綁定了scroll。你的div是否設置爲overflow:scroll

如果是(或者,如果你改變你的束縛,讓你使用的窗口並執行不同的計算),你應該能夠做到:

$(document).ready(function(){ 
    $("#page").bind("scroll",function(e){ 
      if($(this)[0].scrollHeight - $(this).scrollTop() < $(this).outerHeight()) 
      { 
       $(this).append("<li></li><li></li><li></li><li></li>"); 
      } 
    }); 
}); 

或者,如果你打電話來想從數據在這一點的服務器:

$(document).ready(function(){ 
    $("#page").bind("scroll",function(e){ 
      if($(this)[0].scrollHeight - $(this).scrollTop() < $(this).outerHeight()) 
      { 
       $.ajax({ 
        url: url 
        context: this, // used so that in success this is your div 
        dataType:html, 
        success: function(data) { 
         $(this).append(data); 
        } 
       }); 
      } 
    }); 
}); 

這裏的關鍵是確保你綁定滾動到它可以綁定到一個元素。