如何强迫Chrome在URL中指定的锚点比上一个滚动栏位置偏爱?

发布于 2025-02-04 12:33:03 字数 682 浏览 5 评论 0原文

在URL中有一个锚的站点上,当您第一次以Chrome访问它时,它会正确地跳到该锚点。

例如: https://plato.stanford.edu/entries/entries/happiness/happiness/happiness/#hybacc

但是,如果您向上或稍微向下滚动,然后刷新页面,则将您带到上次滚动到的位置,而不是在URL栏中指定的锚。

我想拥有一个功能,以确保当您刷新页面时,无论您刚刚滚动的位置,您都会被带回锚点。

我尝试了此代码:


$( document ).ready(function() {     
    if (window.location.href.split('#')[1]) {
        location.href = "#" + window.location.href.split('#')[1];
    }
});

当页面首次加载时,该代码正确地向我弹到了锚点,但随后(违背我的意愿)将我反弹到最后一个滚动栏位置。我如何防止不必要的弹跳到我上次的滚动栏位置?谢谢。

On a site that has an anchor in the URL, when you first go to it in Chrome, it jumps to that anchor properly.

For example: https://plato.stanford.edu/entries/happiness/#HybAcc

But then, if you scroll up or down slightly, and you refresh the page, you are taken to where you had last scrolled to, rather than the anchor specified in the URL bar.

I would like to have the functionality to guarantee that when you go refresh the page, you are taken back to the anchor regardless of where you had just been scrolling.

I tried this code:


$( document ).ready(function() {     
    if (window.location.href.split('#')[1]) {
        location.href = "#" + window.location.href.split('#')[1];
    }
});

The code correctly bounces to me to the anchor when the page first loads, but then it (against my wishes) bounces me to the last scrollbar position. How can I prevent that unwanted bounce to my last scrollbar position from happening? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文