自动滚动到页面底部

683
我有一份问题列表。当我点击第一个问题时,它应该自动将我带到页面底部的特定元素。
如何使用jQuery实现这一功能?

1
请将页面滚动到底部的jQuery代码放在以下位置:https://dev59.com/jG855IYBdhLWcg3wm1m3 - Sudhir Bastakoti
在这里使用仅CSS:https://dev59.com/p2gt5IYBdhLWcg3w7BoE#68874831 - ashleedawg
你尝试过使用内联锚点吗?https://dev59.com/p2gt5IYBdhLWcg3w7BoE#73257737 - thdoan
你尝试过使用内联锚点吗?https://stackoverflow.com/a/73257737/452587 - undefined
31个回答

10

有很多答案试图计算文档的高度。但对我来说,它们都没有正确计算。然而,以下两个方法都有效:

jquery

    $('html,body').animate({scrollTop: 9999});

或者只是js

    window.scrollTo(0,9999);

2
LOL“奏效”了。如果文档超过9999怎么办? - Dan Dascalescu
3
@DanDascalescu 99999 翻译为: @DanDascalescu 99999 - Andrew
1
如果文档超过99999怎么办? - Brendon Muir
2
@BrendonMuir 如果文档长度超过99999,您可以在答案的javascript代码上方定义一个javascript变量,获取文档的动态高度并使用该变量代替硬编码的99999。 - nviens
1
抱歉 @nviens,我只是在跟随 DanDascalescu 的玩笑 :D - Brendon Muir

9

这是我试过并且有效的方法:

预期结果:

  • 无滚动动画
  • 在第一次加载时在页面底部加载
  • 在所有刷新中都在页面底部加载

代码:

<script>
    function scrollToBottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }
    history.scrollRestoration = "manual";
    window.onload = scrollToBottom;
</script>

为什么这种方法可能比其他方法更有效:

像Chrome这样的浏览器具有内置预设,可以记住刷新后你在页面上的位置。仅使用 window.onload 是不起作用的,因为在调用类似下面这行代码之后,你的浏览器会自动将你滚动回到刷新前的位置:

window.scrollTo(0, document.body.scrollHeight);

这就是为什么我们需要添加以下内容:

history.scrollRestoration = "manual";

window.onload 之前禁用该内置功能。

参考资料:

window.onload 的文档:https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload

window.scrollTo 的文档:https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo

history.scrollRestoration 的文档:https://developer.mozilla.org/en-US/docs/Web/API/History/scrollRestoration


1
谢谢 - 在搜索了几天后,这个答案对我很有用。 - Dshiz

8

如果您想滚动到特定的元素,一种简单的方法是:

每当您想向下滚动时,请调用此函数。

function scrollDown() {
 document.getElementById('scroll').scrollTop =  document.getElementById('scroll').scrollHeight
}
ul{
 height: 100px;
 width: 200px;
 overflow-y: scroll;
 border: 1px solid #000;
}
<ul id='scroll'>
<li>Top Here</li>
<li>Something Here</li>
<li>Something Here</li>
<li>Something Here</li>
<li>Something Here</li>
<li>Something Here</li>
<li>Something Here</li>
<li>Something Here</li>
<li>Something Here</li>
<li>Something Here</li>
<li>Bottom Here</li>
<li style="color: red">Bottom Here</li>
</ul>

<br />

<button onclick='scrollDown()'>Scroll Down</button>


这并不简单,需要创建一个“滚动”元素。 - Dan Dascalescu
@DanDascalescu 没错!但是我的代码是有效的,我不认为它应该被投票降低。 - Liam
“可行”并不足够。这个页面上的所有解决方案都在某种程度上“可行”。而且有很多解决方案。读者应该如何决定呢? - Dan Dascalescu

6

您可以将任何id附加到链接元素的href属性以引用它:

<a href="#myLink" id="myLink">
    Click me
</a>

在上面的示例中,当用户点击页面底部的 Click me 时,导航会导航到 Click me 本身。

1
这对我没有用,因为它改变了URL,然后我的Angular应用程序重定向到其他地方! - heman123

4

虽然来晚了,但这里有一些仅使用JavaScript代码将任何元素滚动到底部的简单方法:

function scrollToBottom(e) {
  e.scrollTop = e.scrollHeight - e.getBoundingClientRect().height;
}

4
你可以尝试使用一个不错的JavaScript插件——Gentle Anchors示例:
function SomeFunction() {
  // your code
  // Pass an id attribute to scroll to. The # is required
  Gentle_Anchors.Setup('#destination');
  // maybe some more code
}

测试兼容性:

  • Mac Firefox,Safari,Opera
  • Windows Firefox,Opera,Safari,Internet Explorer 5.55+
  • Linux未经测试,但应至少与Firefox兼容良好

4

要在Selenium中向下滚动,请使用以下代码:

直到底部下拉,滚动到页面的高度。 使用以下JavaScript代码,在JavaScript和React中都可以正常工作。

JavascriptExecutor jse = (JavascriptExecutor) driver; // (driver is your browser webdriver object) 
jse.executeScript("window.scrollBy(0,document.body.scrollHeight || document.documentElement.scrollHeight)", "");

3
这是我的解决方案:
 //**** scroll to bottom if at bottom

 function scrollbottom() {
    if (typeof(scr1)!='undefined') clearTimeout(scr1)   
    var scrollTop = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
    var scrollHeight = (document.documentElement && document.documentElement.scrollHeight) || document.body.scrollHeight;
    if((scrollTop + window.innerHeight) >= scrollHeight-50) window.scrollTo(0,scrollHeight+50)
    scr1=setTimeout(function(){scrollbottom()},200) 
 }
 scr1=setTimeout(function(){scrollbottom()},200)

1
那里到底发生了什么?你能解释一下你的解决方案吗?只有代码的答案是不被鼓励的。 - Dan Dascalescu

2

我放弃了scrollto,转而尝试了锚点方法:

<a href="#target_id_at_bottom">scroll to the bottom</a>

除了这个CSS魅力:
html,
body {
    scroll-behavior: smooth;
}

祝你有美好的一天!


2
如果您想要滚动到任何标签中或附近的ID,那么只需要一行JavaScript代码就可以使用scrollIntoView函数实现。例如,假设您要操作的元素是一个带有ID“mydiv1”的DIV标签。
<div id="mydiv1">[your contents]</div>

那么您需要运行JavaScript命令。
document.getElementById("mydiv1").scrollIntoView();

没有必要使用JQuery。希望这能帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接