简体   繁体   English

在不使用URL的情况下将页面锚点传递到新页面

[英]Passing page anchors to a new page without using URL

I'm currently trying to pass a page anchor from one page, to another, to have the second page scroll smoothly to the correct div. 我目前正在尝试将页面锚从一个页面传递到另一页面,以使第二个页面平滑滚动到正确的div。

Currently the second part of the desired feature works, but I'm looking for a way to pass the anchor to the second page without it appearing in the URL. 目前,所需功能的第二部分可以使用,但是我正在寻找一种将锚传递到第二页而不将其显示在URL中的方法。

So for example, instead of having 因此,例如,

www.example.com/blog#features

The anchor tag #features, could be passed somehow to the blog page and scroll correctly without it appearing in the URL. 锚标签#features可以以某种方式传递到博客页面并正确滚动,而不会出现在URL中。

Is this possible with jQuery or some form of Ajax style request? jQuery或某种形式的Ajax样式请求是否可能?

try to use localStorage for example http://devdocs.io/dom/window/localstorage 尝试使用localStorage例如http://devdocs.io/dom/window/localstorage

window.localStorage.setItem('id_of_element_to_scroll', '#example-div');

Then you could read from it and scroll into view. 然后您可以从中读取并滚动到视图中。

$(document).ready(function () {
  var idToScroll = localStorage.getItem('id_of_element_to_scroll');
  $('html, body').animate({
    scrollTop: $(idToScroll).offset().top
  }, 200); // with animation
})

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

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