javascript 页面刷新后禁用浏览器的自动滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18617367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:36:42  来源:igfitidea点击:

Disable brower's auto scroll after a page refresh?

javascriptgoogle-chromescroll

提问by Anthony

Is there a way to disable the behavior where some modern browsers (Chrome and Safari) remember your scroll position on a page refresh?

有没有办法禁用某些现代浏览器(Chrome 和 Safari)在页面刷新时记住您的滚动位置的行为?

回答by Cory Duncan

For browsers that support history.scrollRestoration, the auto scroll behavior can be turned off:

对于支持 history.scrollRestoration 的浏览器,可以关闭自动滚动行为:

if ('scrollRestoration' in history) {
  history.scrollRestoration = 'manual';
}

source: https://developers.google.com/web/updates/2015/09/history-api-scroll-restoration

来源:https: //developers.google.com/web/updates/2015/09/history-api-scroll-restoration

回答by Robert

Have you tried firing this after document is ready?

您是否尝试在文档准备好后触发此操作?

$(document).ready(function(){
    window.scrollTo(0, 0);
});

if that does not work...

如果那不起作用...

$(document).ready(function(){
    setTimeout(function(){
        window.scrollTo(0, 0);
    }, 1);
});

Which will push this to the bottom of the call stack

这将把它推到调用堆栈的底部

回答by HIRA THAKUR

not just for chrome,but for all i think this will work well.

不仅适用于 chrome,而且我认为这会很好用。

window.onload = function () {
    window.scrollTo(0, 0);
};

After update of your question:

更新您的问题后:

I think its better if we use some cookies or session storage.

我认为如果我们使用一些 cookie 或会话存储会更好。

回答by Anthony

Instead of hoping a setTimout ends up at the bottom of the stack - I rather enforce it that we hit the scroll position we want. I still consider this a hack, I was hoping for some kind of browser event we bind to.

与其希望 setTimout 最终出现在堆栈的底部 - 我宁愿强制它我们点击我们想要的滚动位置。我仍然认为这是一个黑客,我希望我们绑定到某种浏览器事件。

var scrollToYPos = 100;
var interval = setInterval(checkPos, 0);

function checkPos() {
    if ($(window).scrollTop() == scrollToYPos) {
        clearInterval(interval);
    } else {
        window.scrollTo( 0, scrollToYPos );
        checkPos();               
    }
}      

回答by Skone

I encountered this same issue. Here's the basic solution I came up with:

我遇到了同样的问题。这是我想出的基本解决方案:

      // scroll the user to the comments section if we need to
      wt = win.scrollTop();
      wb = wt + win.height();
      // if scroll position is 0, do this (it's a fresh user), otherwise
      // the browser is likely to resume the user's scroll position, in 
      // which case we don't want to do this
      yab.loaded().done(function() {
        // it seems that browsers (consistently) set the scroll position after
        // page load.  Accordingly wait a 1/4 second (I haven't tested this to the lowest
        // possible timeout) before triggering our logic
        setTimeout(function() {
          // if the window top is 0, scroll the user, otherwise the browser restored
          // the users scroll position (I realize this fails if the users scroll position was 0)
          if(wt === 0) {
            p = self.container.offset().top;
            if(wb != p) {
              win.scrollTop(p - th - 20);         
            }
          }              
        }, 250);
      });

回答by Keith Sunshine Perfetti

I think the easiest way would be to trick the browser into a reload that it thinks is a new page.

我认为最简单的方法是诱使浏览器重新加载它认为是新页面的页面。

window.location = window.location

All the browsers I've tested this in it works consistently. I would personally stay away from onload callbacks as they can cause jumps during load that aren't too visually appealing.

我在其中测试过的所有浏览器都能正常工作。我个人会远离 onload 回调,因为它们可能会在加载过程中导致视觉上不太吸引人的跳跃。