最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery - move to a specific element location - Stack Overflow

programmeradmin3浏览0评论

I have a pagination system like this:

[content]
1 2 3 Next >

when you click on 1/2/3/next the [content] element gets replaced trough ajax.

the problem is that the screen focus moves up if the new height of the [content] is larger than the previous one.

can I force the screen to stay focused where where the pagination links are?

I have a pagination system like this:

[content]
1 2 3 Next >

when you click on 1/2/3/next the [content] element gets replaced trough ajax.

the problem is that the screen focus moves up if the new height of the [content] is larger than the previous one.

can I force the screen to stay focused where where the pagination links are?

Share Improve this question edited Jul 9, 2010 at 3:32 Brock Adams 93.5k23 gold badges240 silver badges304 bronze badges asked Jul 6, 2010 at 13:04 AlexAlex 67.7k185 gold badges459 silver badges650 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

You need to find the position of the element and scroll to that position each time the height changes. Something like:

var p = $(".links").position();
$(window).scrollTop(p.top);

To make the page transparently appear to stay in the same place, you'll need to find out where the links are before you load in the new content.

var before = $(".links").offset().top;

Then after the new content is loaded, get the link's new height.

var after = $(".links").offset().top;

Then do the math to find out how much it's moved.

var difference = after - before

And update your window's scroll location accordingly

$(window).scrollTop( $(window).scrollTop() + difference )

You can probably fix this without jQuery or javascript... Just create a named anchor where you want the top of the page to be after the user clicks the navigation links, put the name of the anchor in the fragment identifier of the nav links, and don't cancel the event so the page navigates to the anchor.

<a name="contentTop"></a>

[content]

<a href="#contentTop" onclick="nextPage(); return true;">Next</a>
发布评论

评论列表(0)

  1. 暂无评论