Make a "Scroll To Next Article" Button with jQuery: jQuery

Make a "Scroll To Next Article" Button with jQuery

Category: JavaScript & jQuery Tags: jQuery | Written on Jul 18, 2008

If you view my articles page, you will see a, "next" button on the bottom left of the browser window that looks like this:
Click the arrow, and it will take you to the next article on the page.

To create this, first lets setup the CSS.  The key in here is the position: fixed; bottom: 2%; left: 2%; statement.  This pins the arrow to the bottom left of the browser window.  Since position fixed doesn't work in IE6, I just hide the div with the * html hack - I know I'm bad ;)  Since Apple dropped support for IE6, I might as well for the advanced features.

Css:
  1. #next_arrow {
  2.     width: 50px;
  3.     padding-top: 50px;
  4.     background: url(../img/structure/backgrounds/next_arrow.gif) no-repeat top left;
  5.     text-align: center;
  6.     position: fixed;
  7.     bottom: 2%;
  8.     left: 2%;
  9.     z-index: 999;
  10. }
  11. * html #next_arrow {
  12.     display: none;
  13. }
  14. #next_arrow:hover {
  15.     cursor: pointer;
  16. }

Using the scrollTo plugin, this is a fairly straight forward task to make the window scroll to the next article.  Here is the code I am using:

JavaScript:
  1. jQuery(function($){
  2.        
  3.     $('<div id="next_arrow">Next</div>')
  4.         .prependTo("body") //append the Next arrow div to the bottom of the document
  5.         .click(function(){
  6.             scrollTop = $(window).scrollTop();
  7.             $('#content h2').each(function(i, h2){ // loop through article headings
  8.                 h2top = $(h2).offset().top; // get article heading top
  9.                 if (scrollTop < h2top) { // compare if document is below heading
  10.                     $.scrollTo(h2, 800); // scroll to in .8 of a second
  11.                     return false; // exit function
  12.                 }
  13.             });
  14.         });
  15.    
  16. });

If you read my comments in the code, you should be able to know what's going on here. The only thing you have to worry about is changing the selector #content h2 to your article heading selector.

That's it, now you have a skip to next article button!

Comments

#1. Andy Matthews http://www.commadelimited.com on Jul 18, 2008
Pretty nice. Treehugger.com has similar functionality, they just use anchor links though. One thing you might consider is adding in functionality that if the user is at the last article on the page, clicking the arrow takes them to the next page of articles.
#2. Sunny on Jul 18, 2008
Nice script that works just as advertised! But doesn't prependTo() add to the beginning of the element rather than adding to the end (appendTo())?
#3. Marc Grabanski http://marcgrabanski.com on Jul 18, 2008
.prependTo("body") does attach the button div to the beginning of the document, but it doesn't matter because the CSS rule I use: position: fixed; bottom: 2%; left: 2%; makes it display on the bottom-left of the page regardless.
#4. J A R on Jul 19, 2008
Perhaps you could provide a non-javascript fallback by having an arrow next to each heading that hash(#) linked to the id of the next heading. The javascript enhancement would remove those and use the fixed arrow. Overly complex? Just thinkin' that's all.
#5. Marc Grabanski http://marcgrabanski.com on Jul 19, 2008
J A R: Good idea, I thought of something similar but I guess my goal here was to add a simple JavaScript enhancement to my website that anyone can use for themselves.
#6. Scriptov on Aug 12, 2008
Shouldn't this:
scrollTop = $(window).scrollTop();
be this? var scrollTop = $(window).scrollTop();

Just my 2 cents ;-)
#7. Marc Grabanski http://marcgrabanski.com on Aug 12, 2008
Scriptov: I guess "technically" you are right, but I don't see why it matters.
#8. jonas on Nov 18, 2008
Thanks! I've been looking for a good implementation of this for loong time.

So thanks, works great!
#9. Jonas on Nov 18, 2008
but one question..,

how would you reverse it? so you get a prev button on the right side..?
#10. Marc http://marcgrabanski.com on Nov 19, 2008
Try this... first add the reverse plugin to reverse the headings:
jQuery.fn.reverse = function()
{
return this.pushStack(this.get().reverse(), arguments);
};

Then switch the less than sign to the greater than sign in the each.
j$('#content h2').reverse().each(function(i, h2){ // loop through article headings
h2top = $(h2).offset().top; // get article heading top
if (scrollTop > h2top) { // compare if document is above heading
$.scrollTo(h2, 800); // scroll to in .8 of a second
return false; // exit function
}
});
#11. Jonas http://pandl.org/wp/ on Feb 13, 2009
Thanks Marc, Works perfectly!

see it in action here, http://pandl.org/wp/

#12. Marc Grabanski http://marcgrabanski.com on Feb 14, 2009
Nice! I like the hot-key as well.
#13. Kristof http://www.nephilistic.com/ on May 02, 2009
Thanks a lot for the code, this was right what i was looking for.
I'm using it on my website now (it's not completely done yet, but I'm getting to it)
http://www.nephilistic.com/
#14. Jonas on Jun 02, 2009
So, now for a little bit more tricky..,

how would you get a next-article button if it's not a vertical scroll.. but a horizontal scroll..?
so i imagine you could use the offset().left instead of top, but you'd still need to horizontally align the content to the middle, which would be possible with an offset based on browser window..?

h2top = $(h2).offset().left+0.5 x 'browser-window-width'; // get article heading top

Any ideas?
#15. Mike on Oct 24, 2009
How do you make the prev not appear at the top?

I want to have the up dissapear when at the top and down when at the bottom..

Thanks

Mike
#16. Evan on Nov 30, 2009
This script is exactly what I am looking for on a current project, however we are loading the 1.3.2 JQuery library. Any chance you could help with an update that is compliant with the current release?

Leave a Comment

Other Reading - Categories