简体   繁体   English

防止页面重新加载后滚动

[英]prevent scroll after page reloading

I save current scroll position using the following way: 我使用以下方式保存当前滚动位置:

$(window).on('unload', function() {

    if ($(window).scrollTop() != 0) {
        localStorage.setItem('lastScrollPosition', $(window).scrollTop());
        $(window).scrollTop(0);
    }

});

After page reloading I load images and I need to scroll at lastScrollPosition only when all images are loaded. 页面重新加载后,我加载了图像,并且仅在加载所有图像时才需要在lastScrollPosition上滚动。 So I call $(window).scrollTop(lastScrollPosition) after all images are loaded, browser scrolls at lastScrollPosition , but after that browser also scrolls at top of page (like $(window).scrollTop(0); ), therefore, I get these action: $(window).scrollTop(lastScrollPosition) -> $(window).scrollTop(0) 所以我在加载所有图像后调用$(window).scrollTop(lastScrollPosition) ,浏览器在lastScrollPosition滚动,但是在该浏览器也滚动到页面顶部之后(例如$(window).scrollTop(0); ),因此,我得到以下动作:$(window).scrollTop(lastScrollPosition)-> $(window).scrollTop(0)

Could you please advise how to fix my issue? 您能否建议如何解决我的问题?

I found out the reason of the issue. 我找出了问题的原因。 This way works correctly, but a instagram library scrolls to top after my scroll. 这种方法可以正常工作,但是instagram库在我滚动后滚动到顶部。 So this library has strange behavior. 因此,该库具有奇怪的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM