Get current scroll position and pass it as a variable with a link?

坚强是说给别人听的谎言 提交于 2019-11-28 18:48:49
  1. Reload the items using AJAX instead of reloading the whole page.

  2. Get and set the scroll position using window.pageYOffset and window.scrollTo(0, y).

    I'd store the position in the hash of the URL:

    // after loading the document, scroll to the right position
    // maybe location.hash has to be converted to a number first
    $(document).ready(function() {
        window.scrollTo(0, location.hash);
    });
    
    // I'm not quite sure if reload() does preserve the hash tag.
    location.hash = window.pageYOffset;
    location.reload();
    

As gs said add the elements via an ajax call instead of of reloading the page.

If you don't want to to use the jQuery.ScrollTo plugin. Which supports everything related to scrolling you could ever have dreamed about

I think what you need is just to pass an HTML anchor at the end of your link. The browser will scroll automatically to the element right next to the matched anchor. Example:

<a href="page2.html#myanchor">Next page</a>

At some point in the middle of page2:

<a name="myanchor">My item N</a>

according to @Shawn Grigson's link, pageYOffset is not supported the same across all browsers.

a jQuery solution (hopefully truly cross-browser compatible, though i haven't yet tested it) for getting and setting an element's vertical scroll is scrollTop().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!