Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

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

I'll be honest, I'm way out of my depth here. I've created a pagination system where you click on a link it reloads the page and a certain number of list items are shown in an unordered list. When you click it again it'll reload the page and more items showing. Think how Twitter shows the tweets in your feed.

The problem is when it reloads the page it stays at the top. What I would like is for it to jump to the same position on the page it previously had.

I already have a selector for the link set up fine I just don't know how to get the current scroll position and then pass it to the new page for it to jump to.

Here's my code so far:

jQuery(document).ready(function(){
    $("a[rel=more]").live("click", function(){
        //script...
    });
});

Where do I go from here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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();
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[2] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

56.8k users

...