首页 > 解决方案 > 文档准备就绪时窗口不会滚动到底部

问题描述

我有一个带有一些 jQuery 的网页,它应该在页面加载时滚动到底部:

<script>
    $(document).ready(function() {
        $("html, body").scrollTop($(document).height());
    });
</script>

由于某种原因,当页面加载时,它不会滚动到底部。但是当我这样做时:

<form id="messageinput">
    <input type="text" name="message">
    <input type="submit" value="">
</form>
<script>
    $("#messageinput").submit(function() {
        $("html, body").scrollTop($(document).height());
    });
</script>

它完美地工作。知道如何解决这个问题吗?有没有更好的方法让它滚动到底部?谢谢!

标签: jqueryhtml

解决方案


如果您想要不会突然停止的动画,请尝试以下操作:

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

如果您不关心动画,请使用:

$(myScrollingElement).scrollTop(Number.MAX_SAFE_INTEGER);

推荐阅读