Use your left/right keys to browse tutorials
jQuery page scrolling

jQuery page scrolling

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on June 4, 2010

Over a year ago I wrote an article on scrolling a page using Prototype, just to update things here I’m going to run through scrolling a page using jQuery and the animate() function. Automated page scrolling is useful in many cases. A good example of this technique can be found in the personal project i worked on a few months back ‘We Deliver’ which you can find here. The page scroll in this project was particularly important as it’s a single page site in that all the content and animation happens within a single user experience.

The Code

It’s extremely easy to get started using jQuery scroll. First we need to declare the element that the vertical scrollbar uses as it’s reference so in other words we want to scroll the main html body window for example.

$('html, body').animate({

The next line is saying scroll to the element defined (in this case the element .bottom) we’re not defining an offset so it takes the top of the element.

scrollTop: $(".bottom").offset().top

The vertical scroll position is the same as the number of pixels that are hidden from view above the scrollable area. If the scroll bar is at the very top, or if the element is not scrollable, this number will be 0.


$("document").ready(function() {

				$('.top-title').click(function(){

					$('html, body').animate({
						scrollTop: $(".middle").offset().top
					}, 2000);

				 });

				$('.middle-title').click(function(){

					$('html, body').animate({
						scrollTop: $(".bottom").offset().top
					}, 2000);

				 });

					$('.bottom-title').click(function(){

					$('html, body').animate({
						scrollTop: $(".top").offset().top
					}, 2000);

				 });

});



Recent shares

More tutorials from Papermashup
Comments
35 discussions around jQuery page scrolling
  1. jquery guide says:

    its very useful codes, i will update in future.

  2. Dale Knight says:

    My page isn’t scrolling to quite the right place,

    how do I alter the scroll in pixels?

  3. Wind says:

    How do I use this when I’ve got tons of links? Instead of defining a whole new function for each link there’s gotta be a way to fetch the href target for each link and scroll to the target div?

    section1
    section2
    section3

    asdfsadfasdf
    asdfsadfasdf
    asdfsadfasdf

  4. Awesome job man, thank you!

  5. rodolfo says:

    HI, is possible giftme a copy of http://wedeliver.papermashup.com/ . please send me site clone for upload to my site with every modifications.
    My email rseales[at]yahoo[dot]com Thanksyou

  6. Pingback: jQuery page scrolling « izzrael: Que Pasa?

  7. Pingback: Great jQuery plugins for fresh websites | Enugu Digit

  8. Vladimir Volek says:

    If you have problem with screen after clicking.

    try return false;

    example:

    $(‘.blog’).click(function(){
    $(‘html, body’).animate({
    scrollTop: $(“#blog”).offset().top
    }, 1000);
    return false;
    });

  9. Pingback: Great jQuery plugins for fresh websites | sushil10018

  10. Pingback: Great jQuery plugins for fresh websites | Blue Sparrow Media

  11. Pingback: Great jQuery plugins for fresh websites | AsterHost

  12. Pingback: Great jQuery plugins for fresh websites

  13. Pingback: Great jQuery plugins for fresh websites | CSS Tips

  14. Pingback: Great jQuery plugins for fresh websites | CSS Tips

  15. Pingback: Great jQuery plugins for fresh websites | Webdesigner Depot

  16. vb078 says:

    Now I found two solutions thks :)

  17. Sean says:

    This is one of the most user friendly jquery plug-ins I’ve ever come across. Simple to implement & easy to adjust.
    Thanks.

  18. clau says:

    Many thanks it works perfectly!!

  19. Andres says:

    Hey Vindikato:
    $(function() {
    $(‘.nav a’).bind(‘click’,function(event){
    var anchor = $(this).attr(‘href’);

    $(‘html, body’).stop().animate({
    scrollLeft: $(anchor).offset().left
    }, 500);
    event.preventDefault();
    });
    });

    • Mobarak says:

      Cierto pero fijate en la posciif3n de chico que este1s de espaldas a nosotros .La cabeza este1 cabizbajo-Algo debe ir mal para que tenga esa postura.Pero totalmente cierto he intrepretado el cuadro en funcif3n del libre albedredo no obstante creo que tus aformaciones son ciertas.

  20. Vindikato says:

    Very, very, very goooood stuff!

    I search a Horizontaly option… that is posible rigth here?
    Thanks :)

  21. Jonny says:

    I’m sorry to sound really amateur here but…
    How would I make the click function work with a <a href .. hyperlink …

    I see you've used Div classes and made them clickable.

    Help me out?

  22. Thanks a lot men! saludos de Mexicali, Mexico :)

  23. Isi says:

    How would you use this in a naviagation?

  24. Ben says:

    Thanks Ashley. Came in quite handy. Appreciate your help.

  25. Jonas says:

    Strange behavior in IOS4 / Ipad – any ideas?

  26. Vladimir Halinen says:

    Hi!

    This works otherwise nicely, but I’m experiencing weird flashes before it starts to scroll. The flashing happens when I click the link. Any ideas why this happens? Ohter than that, it works great. Thanks!

  27. This works great for a project I’m working on. It looked very smooth on FireFox and Safari.

  28. Pingback: Tweets that mention jQuery page scrolling | Papermashup.com -- Topsy.com

  29. CoryMathews says:

    Works great in chrome however In Opera it jumps to the top of the screen before beginning to scroll. Not sure why that is.

  30. Very nice! I can think of at least 3 areas where this functionality could be applied. Now, if only I can convince our fearless leaders to take something like this on…
    Thanks for posting!

  31. Pingback: Tweets that mention jQuery page scrolling | Papermashup.com -- Topsy.com

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.