简体   繁体   English

如何记住资产净值的滚动位置(固定/溢出-y:滚动)

[英]How to Remember Scroll Position of a NAV (fixed/overflow-y:scroll)

I have a NAV position: fixed; top:115px; width:210px; margin-bottom: 0px; bottom: 0px; overflow: hidden; overflow-y:scroll; 我有一个资产净值position: fixed; top:115px; width:210px; margin-bottom: 0px; bottom: 0px; overflow: hidden; overflow-y:scroll; position: fixed; top:115px; width:210px; margin-bottom: 0px; bottom: 0px; overflow: hidden; overflow-y:scroll; specifies. 指定。

<nav itemscope="" itemtype="http://schema.org/SiteNavigationElement" class="content-left">

This NAV has a list of topics(link) in my website. NAV在我的网站上有一个主题列表(链接)。

I want it remember the vertical scroll position of NAV after refreshing page or clicking a link in the page. 我想让它在刷新页面或点击页面中的链接后记住NAV的垂直滚动位置。 I can use jquery. 我可以使用jquery。 How can i do it? 我该怎么做? Thanks. 谢谢。

You will need to store the data locally (Using local storage /cookies) and then re-scroll to that location. 您需要在本地存储数据(使用本地存储 / cookie),然后重新滚动到该位置。

Some example code (customise to your use, this wont work inside StackOverflow as local storage is disabled for snippets): 一些示例代码(根据您的使用进行自定义,这在StackOverflow中不起作用,因为对代码段禁用了本地存储):

 $(document).ready(function(){ // Load any previous scroll position var previousScroll = localStorage.getItem('scrollPos'); if(previousScroll !== null){ $(window).scrollTop(previousScroll) } // Save scroll position when a user stops scrolling $(window).scroll(function() { clearTimeout($.data(this, 'scrollTimer')); $.data(this, 'scrollTimer', setTimeout(function() { localStorage.setItem('scrollPos', $(window).scrollTop()); }, 250)); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div></div> 

Obtaining the scroll position: 获取滚动位置:

Use element.scrollTop . 使用element.scrollTop

var scrollPos = nav.scrollTop;

Storing information when changing the page: 更改页面时存储信息:

If you are really changing the page, then you need to store this information in a session variable or cookie. 如果您确实在更改页面,则需要将此信息存储在会话变量或cookie中。 To do this, you'll need to update the cookie every time the position is changed (or change the session variable with an AJAX request). 为此,您需要在每次更改位置时更新cookie(或使用AJAX请求更改会话变量)。

You can also "pretend" to change the page, like Gmail does. 您也可以“假装”更改页面,就像Gmail一样。 To do this, you'll need to update the browser bar with javascript and change the page's contents using AJAX. 为此,您需要使用javascript更新浏览器栏并使用AJAX更改页面内容。

Loading the scroll position: 加载滚动位置:

Again, you can use element.scrollTop , but assign it: 同样,您可以使用element.scrollTop ,但是分配它:

nav.scrollTop = storedScrollPosition;

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

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