我有一個div溢出:滾動。如何使用JQuery檢查div是否一直滾動到底部?
我想知道它是否一直向下滾動。如何使用JQuery?
這一個不工作:How can I determine if a div is scrolled to the bottom?
我有一個div溢出:滾動。如何使用JQuery檢查div是否一直滾動到底部?
我想知道它是否一直向下滾動。如何使用JQuery?
這一個不工作:How can I determine if a div is scrolled to the bottom?
這裏是正確的解決方案(jsfiddle)。代碼簡述:
$(document).ready(function() {
$('div').bind('scroll', chk_scroll);
});
function chk_scroll(e) {
var elem = $(e.currentTarget);
if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {
console.log("bottom");
}
}
有關更多信息,請參見this。
你可以做到這一點的
(scrollHeight - scrollTop()) == outerHeight()
申請所需的jQuery語法,當然...
下面是代碼:
$("#div_Id").scroll(function (e) {
e.preventDefault();
var elem = $(this);
if (elem.scrollTop() > 0 &&
(elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight())) {
alert("At the bottom");
}
});
Since 2012火狐包含scrollTopMax
財產。如果scrollTop === scrollTopMax
您位於元素的底部。
有趣的是,雖然不被任何瀏覽器支持(並且看起來不會像以前那樣?) – 2015-08-29 05:55:48
function isScrolledToBottom(el) {
var $el = $(el);
return el.scrollHeight - $el.scrollTop() - $el.outerHeight() < 1;
}
這是@ samccone的答案的變體,它結合了@ HenrikChristensen關於子像素測量的評論。
沒有jQuery的,對於onScroll事件
var scrollDiv = event.srcElement.body
window.innerHeight + scrollDiv.scrollTop == scrollDiv.scrollHeight
由於工作沒有jQuery的那樣:
var isBottom = node.scrollTop + node.offsetHeight === node.scrollHeight;
我做的:
var node = $('#mydiv')[0]; // gets the html element
if(node) {
var isBottom = node.scrollTop + node.offsetHeight === node.scrollHeight;
}
對我來說$el.outerHeight()
給出了錯誤的值(由於邊界寬度),而$el.innerHeight()
給出了正確的一個,所以我用
function isAtBottom($el){
return ($el[0].scrollHeight - $el.scrollTop()) == $el.innerHeight();
}
發現這個通過使用谷歌搜索,很有意思http://yelotofu.com/2008/10/jquery-how-to-tell-if-youre-scroll-to-bottom/ – 2011-04-29 05:39:34
你能解釋一下什麼發生在你提供的鏈接不起作用的地方?也許會添加一些標記呢? – 2011-04-29 05:48:00