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
1.3k views
in Technique[技术] by (71.8m points)

jquery - How can I make a link go to the next anchor on the page without knowing anchor name?

I have a button at the top of the page, and I want to code it to proceed to the next anchor regardless of what it is. Actually I will want to do next / previous buttons to go to next and previous anchors. This button is on a static frame so it should scroll the rest of the page ideally.

Any suggestions would be great. Again I won't know what the anchor is, this is more like a chapter navigation using a generic button.

Seems like finding next anchor object in the dom and go there should be a doable thing but I'm not a whiz in jquery or javascript to do that.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
var index = -1;

$('.next').click(function() {
   index++;
   $(window).scrollTop($('a').eq(index).position().top);

});
$('.previous').click(function() {
   index--;
   if(index < 0) { index = 0;}

   $(window).scrollTop($('a').eq(index).position().top);
});

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

...