我的页面上有一个div:

<div id='div1' style='overflow:scroll;overflow-x:hidden;max-height:200px;'></div>

我怎样才能使div滚动到div的底部??不是页面,只是DIV。


当前回答

更新:请参阅Mike Todd的解决方案以获得完整的答案。


$("#div1").animate({ scrollTop: $('#div1').height()}, 1000);

如果你想让它变成动画(超过1000毫秒)。

$('#div1').scrollTop($('#div1').height())

如果你想要即时的。

其他回答

你可以检查scrollHeight和clientHeight与scrollTop滚动到div底部像下面的代码。

$ (' # div ')。滚动(函数(事件){ if ((parseInt($('#div')[0].scrollHeight) - parseInt(this.clientHeight)) == parseInt($('#div').scrollTop())) { Console.log("这是div的滚动底部"); } });

试试这个:

$('#div1').scrollTop( $('#div1').height() )
$(window).load(function() {
  $("html, body").animate({ scrollTop: $(document).height() }, 1000);
});

这将获取页面的高度,并在窗口加载后向下滚动页面。一旦页面准备好,将1000更改为您需要的任何速度。

当页面加载时,滚动是最大的值。

这是消息框,当用户发送消息时,总是显示最新的聊天,所以滚动值总是最大的。

$('#message').scrollTop($('#message')[0].scrollHeight);

见图片

我在这里看到的所有答案,包括目前“被接受”的答案,实际上都是错误的,因为他们设置了:

scrollTop = scrollHeight

而正确的方法是设置:

scrollTop = scrollHeight - clientHeight

换句话说:

$('#div1').scrollTop($('#div1')[0].scrollHeight - $('#div1')[0].clientHeight);

或动画:

$("#div1").animate({
  scrollTop: $('#div1')[0].scrollHeight - $('#div1')[0].clientHeight
}, 1000);