我們有一個div,我需要找到它的最大滾動高度大小。 我聊天,目前,有12級的消息,有800像素高,但是當我使用此代碼:查找滾動div的底部高度?
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;
如果你只是用一個荒謬的大量?我相信瀏覽器實際上不會超出其界限。 – ArrayKnight