Javascript 如何使用jQuery检查DIV是否一直滚动到底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5828275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:05:21  来源:igfitidea点击:

How to check if a DIV is scrolled all the way to the bottom with jQuery

javascriptjquery

提问by TIMEX

I have a div with overflow:scroll.

我有一个溢出的div:滚动。

I want to know if it's currently scrolled all the way down. How, using JQuery?

我想知道它当前是否一直向下滚动。如何,使用 JQuery?

This one doesn't work: How can I determine if a div is scrolled to the bottom?

这个不起作用:如何确定 div 是否滚动到底部?

回答by samccone

Here is the correct solution (jsfiddle). A brief look at the code:

这是正确的解决方案(jsfiddle)。简单看一下代码:

$(document).ready(function () {
    $('div').on('scroll', chk_scroll);
});

function chk_scroll(e) {
    var elem = $(e.currentTarget);
    if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {
        console.log("bottom");
    }
}

See thisfor more info.

有关更多信息,请参阅内容。

回答by Chris Martin

function isScrolledToBottom(el) {
    var $el = $(el);
    return el.scrollHeight - $el.scrollTop() - $el.outerHeight() < 1;
}

This is variation of @samccone's answer that incorporates @HenrikChristensen's comment regarding subpixel measurements.

这是@samccone 答案的变体,其中包含了@HenrikChristensen 关于亚像素测量的评论。

回答by Nirmal

You can do that by

你可以这样做

(scrollHeight - scrollTop()) == outerHeight()

Apply required jQuery syntax, of course...

应用所需的 jQuery 语法,当然...

回答by user3548525

Here is the code:

这是代码:

$("#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");
    }
});

回答by Jan Jongboom

Since 2012Firefox contains the scrollTopMaxproperty. If scrollTop === scrollTopMaxyou're at the bottom of the element.

自 2012 年以来,Firefox 包含该scrollTopMax属性。如果scrollTop === scrollTopMax你在元素的底部。

回答by Haven

Without jquery, for onScroll event

没有 jquery,用于 onScroll 事件

var scrollDiv = event.srcElement.body
window.innerHeight + scrollDiv.scrollTop == scrollDiv.scrollHeight

回答by nha

Since it works without jQuery like that :

因为它没有像这样的 jQuery 工作:

 var isBottom = node.scrollTop + node.offsetHeight === node.scrollHeight;

I do :

我愿意 :

 var node = $('#mydiv')[0]; // gets the html element
 if(node) {
    var isBottom = node.scrollTop + node.offsetHeight === node.scrollHeight;
 }

回答by isapir

For me $el.outerHeight()gives the wrong value (due to the border width), whereas $el.innerHeight()gives the correct one, so I use

对我来说$el.outerHeight()给出了错误的值(由于边框宽度),而$el.innerHeight()给出了正确的值,所以我使用

function isAtBottom($el){

    return ($el[0].scrollHeight - $el.scrollTop()) == $el.innerHeight();
}