我如何使用jquery向下滚动到iframe或页面的底部?


当前回答

之前的回答中提到的脚本,比如:

$("body, html").animate({
    scrollTop: $(document).height()
}, 400)

or

((窗口).scrollTop美元(文档).height ());

将不会在Chrome中工作,并将在Safari中跳跃,以防html标签在CSS中溢出:auto;属性集。我花了将近一个小时才弄明白。

其他回答

这个对我很有用:

var elem = $('#box');
if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {
  // We're at the bottom.
}

如果你想要一个漂亮的缓慢动画滚动,对于任何带有href="#bottom"的锚,这将滚动到底部:

$("a[href='#bottom']").click(function() {
  $("html, body").animate({ scrollTop: $(document).height() }, "slow");
  return false;
});

您可以随意更改选择器。

scrollTop()返回从可滚动区域的视图中隐藏的像素数,因此给它:

$(document).height()

会超出页面的底部。为了让滚动实际“停止”在页面底部,需要减去当前浏览器窗口的高度。这将允许在需要时使用easing,所以它变成:

$('html, body').animate({ 
   scrollTop: $(document).height()-$(window).height()}, 
   1400, 
   "easeOutQuint"
);

之前的回答中提到的脚本,比如:

$("body, html").animate({
    scrollTop: $(document).height()
}, 400)

or

((窗口).scrollTop美元(文档).height ());

将不会在Chrome中工作,并将在Safari中跳跃,以防html标签在CSS中溢出:auto;属性集。我花了将近一个小时才弄明白。

$('.block').scrollTop($('.block')[0].scrollHeight);

我使用此代码在新消息到达时滚动聊天。