jQuery:返回上一页时如何返回到完全相同的滚动位置 [英] JQuery: How to return to the exact same scroll position when going back to previous page
本文介绍了jQuery:返回上一页时如何返回到完全相同的滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的项目列表很长,当我单击每个项目并返回到主列表时,滚动位置丢失了.
I have a long list of items and when I clicked into each item and return to the main list, the scroll position was lost.
如何使用jQuery返回相同的确切滚动位置?有什么简单的方法吗?
How can I return to the same exact scroll position using jQuery? Is there any easy way to do it?
$(document).ready(function() {
$('.update-button').click(function (){
sessionStorage.scrollPos = $(window).scrollTop();
console.log(sessionStorage.scrollPos);
});
});
var init = function () {
//get scroll position in session storage
$(window).scrollTop(sessionStorage.scrollPos || 0)
};
window.onload = init;
以上是我的代码的样子.我尝试记录位置,sessionStorage.scrollPos为0.我很确定我将页面滚动到某个位置.
Above is what my code looks like. I tried to log the position and sessionStorage.scrollPos was 0. I am pretty sure I scrolled the page to somewhere.
非常感谢您的帮助.
推荐答案
您需要将滚动位置的值存储在sessionStorage(或任何存储)中,并在页面加载时再次使用它.
You need to store value of scroll position in sessionStorage(or any storage) and re use it again on page load.
$(window).scroll(function () {
//set scroll position in session storage
sessionStorage.scrollPos = $(window).scrollTop();
});
var init = function () {
//get scroll position in session storage
$(window).scrollTop(sessionStorage.scrollPos || 0)
};
window.onload = init;
这篇关于jQuery:返回上一页时如何返回到完全相同的滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文