简体   繁体   English

加载了动态Javascript内容后跳转到最后一个滚动位置

[英]Jump to last scroll position when dynamic Javascript content has loaded

I have a very long (y axis) page with a lot of stuff on it that is dynamically added via JavaScript. 我的页面很长(y轴),上面有很多东西,这些东西是通过JavaScript动态添加的。 When the page loads its height is almost 200px and when it has finished JavaScript initialization its height could be up to 20 times the screen height. 当页面加载时,其高度接近200像素,并且在完成JavaScript初始化后,其高度可能高达屏幕高度的20倍。

In the normal usages the user may reload the page using the browsers reload button or shortcut. 在正常使用中,用户可以使用浏览器的“重新加载”按钮或快捷方式重新加载页面。 After reload the page is not yet at full height when the browser tries to jump to the last scroll position and thus lands at the start position instead. 重新加载后,当浏览器尝试跳至最后一个滚动位置并因此着陆到起始位置时,页面尚未达到完整高度。 Only the client side Javascript is aware of the content height and scroll position. 仅客户端Javascript知道内容的高度和滚动位置。

I want it to scroll to the last scroll position when my JavaScript took care of loading the content without having to store the users last scroll position or content height anywhere (eg local storage, cookie, URL, on the server, etc.). 当我的JavaScript负责加载内容时,我希望它滚动到最后一个滚动位置而不必将用户的最后一个滚动位置或内容高度存储在任何位置(例如,本地存储,cookie,URL,在服务器上等)。

Is that somehow possible? 有可能吗?

When the user clicks off the page, store the value of window.pageYOffset in local storage. 当用户单击页面时,将window.pageYOffset的值存储在本地存储中。 Then, once they return to the page, load the value if it exists, then once all elements have been initialized, use window.scrollTo(0, pageYOffset) . 然后,一旦它们返回页面,则加载值(如果存在),然后在初始化所有元素后,使用window.scrollTo(0, pageYOffset)

It's not possible to get around storing this information somehow, as it is not a part of the page or the browser. 由于信息不是页面或浏览器的一部分,因此无法四处存储该信息。 Storing information in local storage allows your JavaScript to retain information about the user, specifically in this case where they had scrolled to on the page. 在本地存储中存储信息使您的JavaScript可以保留有关用户的信息,特别是在用户滚动到页面上的情况下。

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

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