gpt4 book ai didi

javascript - 如何将 HTML 页面滚动到给定的 anchor

转载 作者:IT老高 更新时间:2023-10-28 11:03:52 28 4
gpt4 key购买 nike

我想让浏览器将页面滚动到给定的 anchor ,只需使用 JavaScript。

我在我的 HTML 代码中指定了 nameid 属性:

 <a name="anchorName">..</a>

 <h1 id="anchorName2">..</h1>

我希望通过导航到 http://server.com/path#anchorName 来获得与您相同的效果。应滚动页面,使 anchor 靠近页面可见部分的顶部。

最佳答案

function scrollTo(hash) {
location.hash = "#" + hash;
}

根本不需要 jQuery!

关于javascript - 如何将 HTML 页面滚动到给定的 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3163615/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com