重新加载网页后如何保持滚动位置? [英] How to retain scroll position after reloading the web-page too?

查看:26
本文介绍了重新加载网页后如何保持滚动位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用过 Javascript scrollTo 和 scrollBy 函数.但它不起作用

I have used Javascript scrollTo and scrollBy function. But its not working

对我来说.我想存储滚动的位置,以便我可以得到也在加载网页.

for me . I want to store the position of scroll so that I can get after loading the web page too.

推荐答案

可以使用 session storage 来存储位置,然后在页面重新加载时返回位置,如下所示:

You can use session storage to store the position then get back to the position when the page is reloaded, like this:

$(window).scroll(function() {
  sessionStorage.scrollTop = $(this).scrollTop();
});

$(document).ready(function() {
  if (sessionStorage.scrollTop != "undefined") {
    $(window).scrollTop(sessionStorage.scrollTop);
  }
});

这里是 JSFiddle 演示

这篇关于重新加载网页后如何保持滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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