jQuery即使更新后仍滚动到Div的底部? [英] jQuery Scroll to bottom of Div even after it updates?
问题描述
因此,我的页面上有一个像聊天室一样的div,里面充满了文字.它具有固定的高度和宽度(由CSS定义).
So I have a page with a chat-like div that fills up with text. It has a fixed height and width (defined by css).
每次更新时我都需要滚动到底部,这是我到目前为止使用的JS:
I need it to scroll to bottom upon every update, here's the JS I've used so far:
$("#div1").animate({ scrollTop: $('#div1').height()}, 1000);
它工作了一会儿,然后突然停止...我认为这可能与我为div设置的高度有关吗?我不确定.
It works for a while and then suddenly stops... I think it might have something to do with my set height for the div? I'm not sure.
那我该怎么做以确保div向下滚动到其内容的底部(使用jQuery/AJAX函数更新内容)
So what can I do to ensure the div scrolls down to the bottom of it's content (the content is updated using a jQuery/AJAX function)
提前谢谢!
我也尝试过$("#div1").animate({ scrollTop: $('#div1').height()}, 0);
,但是它仍然在特定位置停止滚动...
I've also tried $("#div1").animate({ scrollTop: $('#div1').height()}, 0);
but it still stops scrolling at a certain point...
推荐答案
您是否尝试过使用.scrollHeight
:
$("#div1").animate({ scrollTop: $("#div1")[0].scrollHeight}, 1000);
这篇关于jQuery即使更新后仍滚动到Div的底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!