【发布时间】:2013-06-27 21:14:21
【问题描述】:
我们有一个 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 scrolling up?
基本上我能做的是,在发送消息时,做检查:
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;
【问题讨论】:
-
如果你只是使用了一个大得离谱的数字怎么办?我相信浏览器实际上不会滚动超出它的界限。
标签: javascript jquery