HTML/JS:回到先前的页面并保持原滚动位置

3
让我们假设我有一篇很长的页面,比如一篇文章。有一个链接,比如“购买完整文章”。当点击它时,它将重定向到一个包含所有脚本的页面,将文章添加到数据库中,然后再次重定向回文章页面。这种情况可能吗?还有,如何使它重定向到用户正在浏览的相同滚动位置?
一个很好的例子是浏览器上的“返回”按钮。它会带你回到之前访问的页面,并回到相同的滚动位置。
2个回答

1

来自 这个问题

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"));
   }
});

2
实际上,这样做是行不通的。如果你从第一页跳转到第二页,它会保存第一页的滚动位置,然后如果你从第二页跳转回第一页,它会再次保存第二页的滚动位置,然后将第一页滚动到保存的值。 - undefined

-1
你可以使用纯JavaScript:
window.history.back()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接