Animate page to id with jquery

Simple vertical page scrolling with animation easing using jquery.

DEMO DOWNLOAD

How you do it

Start by loading the jquery library by inserting:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>

between the head tag of your document. This code loads the jquery library straight from Google and we do this for caching purposes.

We also need to insert the jquery plugin that does the easing part (optional but animations with some kind of easing almost always look better)

<script src="jquery.easing.1.3.js" type="text/javascript"></script>

Next we need to insert the actual function that does the trick of animating our document vertically when we click on a link. After the jquery scripts (still inside the head tag) insert:

<script type="text/javascript">
//scroll page to id
function Animate2id(id2Animate){
	var animSpeed=1500; //animation speed
	var easeType="easeInOutExpo"; //easing type
	if($.browser.webkit){ //webkit browsers do not support animate-html
		$("body").stop().animate({scrollTop: $(id2Animate).offset().top}, animSpeed, easeType);
	} else {
		$("html").stop().animate({scrollTop: $(id2Animate).offset().top}, animSpeed, easeType);
	}
}
</script>

This function gets the target id (the container id to scroll to), checks for webkit browsers (that do not support html animation) and animates the document with a nice easing effect that we define in easeType variable. Inside the function we also define the scrolling speed (var animSpeed) in milliseconds.

The css:

body{margin:0; padding:0; background:#fff;}
#menu{position:fixed; z-index: 2; top:10px; left:10px; clear:both; border:1px solid #666; padding:5px; background:#fff;}
#menu a{margin-left:5px; margin-right:5px;}
#c1, #c2, #c3, #c4, #c5 {height:1000px; margin:0 0 60px 0; background:#eee; padding:60px 10px;}

And finally our html code:

<div id="menu">
<a onclick="Animate2id('#c1');return false" href="#">Content 1</a>
<a onclick="Animate2id('#c2');return false" href="#">Content 2</a>
<a onclick="Animate2id('#c3');return false" href="#">Content 3</a>
<a onclick="Animate2id('#c4');return false" href="#">Content 4</a>
<a onclick="Animate2id('#c5');return false" href="#">Content 5</a>
</div>
<div id="content">
<div id="c1">Content 1</div>
<div id="c2">Content 2</div>
<div id="c3">Content 3</div>
<div id="c4">Content 4</div>
<div id="c5">Content 5</div>
</div>

We call Animate2id function with an onclick event attached to a link and send the id of the container we want to scroll to (much like a named anchor). We also insert return false to prevent browser’s default link actions.

You can use this little script to create a menu (like our example) that scrolls page to a desired content area (cool interface for a single page website) or just to make a fancy “back to top” footer link ;)

Easy!

Digg This
Reddit This
Stumble Now!
Bookmark this on Delicious
This entry was posted in jQuery/Javascript, Navigation, Transitions and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

10 Comments

  1. Posted October 21, 2010 at 05:34 | Permalink

    Nice work!

  2. Posted April 17, 2011 at 19:20 | Permalink

    Pretty solid system, thanks!
    Is it also possible to use prev & next button?
    For instance if you are at C3, you could go back to C2 or next to C4…

  3. Antonio
    Posted November 7, 2011 at 21:07 | Permalink

    Muito bom, parabéns pelo tutorial!!!!

    • malihu
      Posted November 9, 2011 at 14:07 | Permalink

      Obrigado!
      Google Translate helps :)

  4. Rish
    Posted November 10, 2011 at 21:09 | Permalink

    I am new to all this just started learning css/js/jquery yesterday. but I was trying the same thing without “easeInOutExpo” and it still works fine, I dint actually try working it with the easeInOutExpo thing cause I think for some reason it is not able to find the function, anyways this is what exactly what I used:

    $(function() {
    $(‘ul.link a’).bind(‘click’,function(event){
    var $anchor = $(this);
    $(‘html, body’).stop().animate({
    scrollTop: $($anchor.attr(‘href’)).offset().top
    }, 3000);
    event.preventDefault();
    });
    });

    It is exactly what you did but this way I don’t have to go every time and call the method everywhere a link is defined, it automatically activates when a link is clicked. I am not sure what adv/disadv this has so please let me know if any and if you can, can you brief me on what difference would it make if I used easeInOutExpo.
    I have included the file but I dont know why it cant read the method, any suggestion there too would be great help.

  5. Frederico
    Posted January 31, 2012 at 09:32 | Permalink

    I am impressed about the reduced code to your above horizontal scrolling example with fullscreen images and next/prev. navigation.

    Do you also have the same with an example of vertical scrolling?
    I would appreciate it to see it in action.
    Gracias!!

  6. ew
    Posted March 13, 2012 at 11:51 | Permalink

    Hi,

    Im struggling to get this to work for my situation…

    I’ve got a jqueryui dialog, on which is a scrollable div. Im trying to get the div to scroll to a certain section using your plugin

  7. Posted March 21, 2012 at 15:13 | Permalink

    Just started delving into this library, thanks for the help.. whats cross browser compatitibility like? I know webkit doesnt support.. thanks in advance! will link you when ive finished building ;)

One Trackback

  1. By Animate page to id with jquery » Web Design on October 25, 2011 at 20:29

    [...] Animate page to id with jquery [...]

Post a Comment

Your email is never published nor shared. 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>