Reload browser does not reset page to top

后端 未结 4 1170
梦毁少年i
梦毁少年i 2020-12-06 02:44

I thought when you clicked refresh, that the browser was supposed to reset your page to the top? I am using a js accordion and when I refresh, it closes the acc

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 03:22

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

    But you can force it with some simple jQuery:

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

    EDIT:

    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);
    });
    

    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?

提交回复
热议问题