jQuery page scrolling

jQuery page scrolling

Posted on June 4, 2010 by Ashley

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);

				 });

});

This entry was posted in Design, Javascript, Learn, Web Tools, jQuery and tagged , , , , , . Bookmark the permalink.
Comments
6 discussions around jQuery page scrolling
  1. Pingback: Tweets that mention jQuery page scrolling | Papermashup.com -- Topsy.com

  2. 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!

  3. 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.

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

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

  6. 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!

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>

Subscribers
1,250
Twitter
510
Comments
1,207
Posts
125