我怎样才能保持一个滚动区域的滚动位置时pressing后退按钮? [英] How can I retain the scroll position of a scrollable area when pressing back button?

查看:110
本文介绍了我怎样才能保持一个滚动区域的滚动位置时pressing后退按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的滚动DIV中的链接一个长长的清单。每次当一个链接,用户点击,然后点击后退按钮,它开始在div的最顶端。它不是用户友好的给我们的用户。什么方法可以让浏览器滚动到previous位置时pressing后退按钮?

I have a long list of links inside a big scrollable div. Each time when a user click on a link then click the back button, it starts at the very top of the div. It is not user friendly to our users. Any ways to let the browser scroll to the previous position when pressing the back button?

非常感谢你!

推荐答案

在页面卸载,获得滚动位置并将其存储在本地存储。然后,页面加载过程中,检查本地存储,并设置滚动位置。假设你有ID的div元素元素。在情况下,它的页面,请更改选择:)

During page unload, get the scroll position and store it in local storage. Then during page load, check local storage and set that scroll position. Assuming you have a div element with id element. In case it's for the page, please change the selector :)

$(function() {
   $(window).unload(function() {
      var scrollPosition = $("div#element").scrollTop();
      localStorage.setItem("scrollPosition", scrollPosition);
   });
   if(localStorage.scrollPosition) {
      $("div#element").scrollTop(localStorage.getItem("scrollPosition"));
   }
});

这篇关于我怎样才能保持一个滚动区域的滚动位置时pressing后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆