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

Make a "Scroll To Next Article" Button with jQuery

Tags: jQuery | Written on 18/7/08

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 on 18/7/08
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 18/7/08
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 on 18/7/08
.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 19/7/08
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 on 19/7/08
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 3 weeks, 3 days ago
Shouldn't this:
scrollTop = $(window).scrollTop();
be this? var scrollTop = $(window).scrollTop();

Just my 2 cents ;-)
#7. Marc Grabanski 3 weeks, 3 days ago
Scriptov: I guess "technically" you are right, but I don't see why it matters.

Leave a Comment