我们有一个 div,我需要找到它的最大滚动高度。我的聊天目前有 12 条消息,高度为 800 像素,但是当我使用此代码时:
var trueDivHeight = $('#chat')[0].scrollHeight;
它只会找出 div 的高度,而不是整个向下滚动,它会说 490px。但我想找到最大高度,到底部。
从顶部到卷轴底部。
为什么我需要这个?由我来解决这个赏金问题:AJAX Chat - Automatically drag the scroll down, but don't drag it when the user is up scrolling?
基本上我能做的是,在发送消息时,检查:
if (checkScroll(trueDivHeight)) { //Runs the function checkScroll with max div height.
scrollDown = true; // turn true if it was true.
}
if (scrollDown) {
setInterval(function () {
scrollToBottom(); // scroll to bottom 1 second after posted message..
}, 1000);
}
以及我可以使用的功能:
function scrollToBottom() {
$('#chat').scrollTop(bottom); // Makes scroll go to the most bottom.
}
function checkScroll(size) {
if ($("#chat").scrollTop() == size) { // Checks if current scroll position equals to the max bottom position.
return true; // if yes, return true.
}
}
但问题是,trueDivHeight 没有找到滚动条的整个高度,从上到下。如果是这样,那么它对我有用。
有任何想法吗?
编辑:
var trueDivHeight = $('#chat')[0].scrollHeight; // finds the max bottom px
var divHeight = $('#chat').height();
var bottom = $('#chat')[0].scrollHeight;