且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重新加载浏览器不会将页面重置为顶部

更新时间:2023-08-31 22:20:46

好吧,正如你所见,它没有 :)

Well, as you can see, it does not :)

但是你可以用一些简单的 jQuery 来强制它:

But you can force it with some simple jQuery:

$(document).ready(function(){
    $(this).scrollTop(0);
});

似乎适用于 IE 9、FF 12 和 Chrome 20.0 的唯一方法如下:

The only way that seems to work in IE 9, FF 12 and Chrome 20.0 is the following:

$(document).ready(function(){
    $('html').animate({scrollTop:0}, 1);
    $('body').animate({scrollTop:0}, 1);
});

奇怪的是,当我尝试直接滚动元素而不应用任何动画(即 $('html').scrollTop(0))时,它不起作用.由于持续时间设置为 1 毫秒,因此用户不会注意到任何事情.

Strange thing is that when I tried scrolling the elements directly without applying any animation (that is, $('html').scrollTop(0)), it didn't work. Since the duration is set to 1 millisecond, the user will not notice anything.

如果有人能对此有所了解,我会很高兴 - 为什么滚动仅适用于动画?

I would be glad if anyone could shed some light on this - why does the scrolling only work with animations?