2010-05-14 96 views
10

所以基本上我的問題是一個看似簡單的問題。jQuery:檢測滾動底部不起作用,只檢測頂部

你可以在http://furnace.howcode.com看到它的行動(請注意,數據是通過Ajax返回的,所以如果沒有任何反應,給它一點時間!)。

想要發生什麼,在第二列中,當您到達滾動底部時,會返回下5個結果。

但實際發生的是它只返回5個結果,當你點擊滾動區域的頂部。嘗試一下:向下滾動,沒有任何反應。向上滾動到頂部,返回結果。

怎麼回事?

這裏是我的代碼,我使用的是:

$('#col2').scroll(function(){ 
    if ($('#col2').scrollTop() == $('#col2').height() - $('#col2').height()){ 
     loadMore(); 
    } 
}); 

loadMore();是獲取數據並追加其功能。

那麼這裏怎麼回事?謝謝你的幫助!

+0

恐怕沒有什麼工作。有人有任何進一步的想法? – Jack 2010-05-14 22:30:36

回答

11

你的數學是錯誤的,你說如果滾動條的位置等於列的高度減去列的高度(它等於零)加載更多。這就是爲什麼你的loadMore()被稱爲你的上面。

嘗試:

$('#col2').scroll(function(){ 
    if ($('#col2').scrollTop() == $('#col2').height()){ 
     loadMore(); 
    } 
}); 
+1

恐怕這不起作用。我手動輸入後複製粘貼,但沒有任何反應。原始URL我得到的原始來源是這樣的:http://www.webresourcesdepot.com/load-content-while-scrolling-with-jquery/ – Jack 2010-05-14 21:40:52

13

這在過去爲我工作。

var col = $('#col2'); 
col.scroll(function(){ 
    if (col.outerHeight() == (col.get(0).scrollHeight - col.scrollTop())) 
     loadMore(); 
}); 

這裏還有一個很好的explanation

+0

我已經看了解釋並嘗試了你的例子 - 但沒有發生。我真的無法理解爲什麼。還有什麼想法? – Jack 2010-05-14 22:16:54

+1

@Jack Webb-Heller - 我可以理解爲什麼。我錯過了一些重要的東西。 'scrollHeight'不能用在jquery對象上。您需要指定'col [0] .scrollHeight'才能使其工作。我編輯的答案包括 – munch 2010-05-14 23:25:01

+1

'col.get(0).scrollHeight'是一個更合適的jQuery方式來處理它 – munch 2010-05-14 23:36:43

1

jordanstephens發佈的解決方案在正確的軌道上。但是,您需要的不是#col2的高度,您需要#col2的父元素的高度。

例子:

$('#col2').scroll(function(){ 
    if ($('#col2').scrollTop() == $('#col2').parent().height()) { 
    loadMore(); 
    } 
} 
+1

父元素基本上是$(document),#col2是頂級標籤。我已經嘗試過,但仍然沒有運氣。我真的不能說太多......因爲沒有任何反應! :S – Jack 2010-05-14 22:16:16

+0

如果父母是文檔,那麼代替$('#col2')。parent()。height()只需使用$(window).height() – Mark 2010-05-18 08:10:30

24

下進行了測試,並能正常工作:

$(window).scroll(function() { 
    if ($(window).scrollTop() == $(document).height() - $(window).height()) { 
    loadMore(); 
    } 
}); 
+1

將函數包裝在[反彈](http://lodash.com/docs#debounce)功能,以提高性能。 – 2012-10-16 02:45:25

+0

+1工程就像一個魅力! – dariush 2014-04-28 21:21:24

1

也許下面的工作:

  1. HTML - 包裝無論是內部#COL2與另一個DIV,說#col2 - 內部
  2. CSS - 設置一個固定heig HT和:僅#COL2
  3. JS '溢出自動' - 見下文插件:

    $.fn.scrollPaging = function (handler) { 
        return this.each(function() { 
         var $this = $(this), 
          $inner = $this.children().first(); 
    
         $this.scroll(function (event) { 
          var scrollBottom = $this.scrollTop() + $this.height(), 
           totalScroll = $inner.height(); 
    
          if (scrollBottom >= totalScroll) { 
           handler(); 
          } 
         }); 
    
        }); 
    }; 
    
    $('#col2').scrollPaging(loadMore); 
    
1

@munch最接近的,但如果你有元件上的邊界,那麼你需要使用.innerHeight()而不是.outerHeight(),因爲後者包含邊框並且.scrollTop()不會(如果有填充,則.height()也會出現)。另外,至少在jQuery 1.7.1以及更早的版本中,我建議使用.prop('scrollHeight'),而不是直接從DOM中檢索它。我發現some talk在IE 5-8上DOM scrollHeight被破壞,jQuery函數爲我抽象,至少在IE8上。

var $col = $('#col2'); 
$col.scroll(function(){ 
if ($col.innerHeight() == $col.prop('scrollHeight') - $col.scrollTop()) 
    loadMore(); 
}); 
+0

您的回答對我來說非常合適,非常感謝:) – Systemfreak 2013-09-27 22:46:43

2

我發現了一個可行的方法。

這些答案都不適用於我(目前在FireFox 22.0中測試),經過大量研究後,我發現,似乎是一個更清潔和直接的解決方案。

實施的解決方案:

function IsScrollbarAtBottom() { 
    var documentHeight = $(document).height(); 
    var scrollDifference = $(window).height() + $(window).scrollTop(); 
    return (documentHeight == scrollDifference); 
} 

資源: http://jquery.10927.n7.nabble.com/How-can-we-find-out-scrollbar-position-has-reached-at-the-bottom-in-js-td145336.html

問候

0

我知道這個問題已經解決了,但是元素使用問題時的jQuery scrollTOp()功能除了上$(window).scrollTop()$(document).scrollTop()由於一些問題,如

jQuery: detecting reaching bottom of scroll doesn't work, only detects the top

Explain why scrollTop() always returns 0 here

$(document).scrollTop() always returns 0

所以,你可以使用的

$(window).scrollTop() - $('your_selector').offset().top 

代替

$('your_selector').scrollTOp() 

避免了jQuery scrollTOp()問題。

我的建議如下:

$(window).scroll(function() { 
     var $more = $('#col2'); 
     var top = $(window).scrollTop() - $more.offset().top; 
     if(top + $more.innerHeight() >= $more[0].scrollHeight) { 
      loadMore(); 
     } 
    });