TIM*_*MEX 38 javascript jquery
我有一个溢出的div:滚动.
我想知道它目前是否一直向下滚动.怎么用JQuery?
这个不起作用:如何确定div是否滚动到底部?
sam*_*one 71
这是正确的解决方案(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");
}
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅此
Chr*_*tin 13
function isScrolledToBottom(el) {
var $el = $(el);
return el.scrollHeight - $el.scrollTop() - $el.outerHeight() < 1;
}
Run Code Online (Sandbox Code Playgroud)
这是@samccone的答案的变体,其中包含@ HenrikChristensen关于亚像素测量的评论.
因为它可以在没有 jQuery 的情况下工作:
var isBottom = node.scrollTop + node.offsetHeight === node.scrollHeight;
Run Code Online (Sandbox Code Playgroud)
我愿意 :
var node = $('#mydiv')[0]; // gets the html element
if(node) {
var isBottom = node.scrollTop + node.offsetHeight === node.scrollHeight;
}
Run Code Online (Sandbox Code Playgroud)