Simple vertical page scrolling with animation easing using jquery.
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!





10 Comments
Nice work!
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…
I haven’t implement an automatic way of scrolling to next/previous id yet. I might give it a go in the future. For now, you can always do it manually
I’ve done some testing with horizontal scrolling with next/previous scrolling. It has some extra stuff like fullscreen background images etc., but to get an idea:
http://manos.malihu.gr/tuts/horizontal_single-page_template/page_srolling.html
Muito bom, parabéns pelo tutorial!!!!
Obrigado!
Google Translate helps
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.
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!!
The equivalent of this script for vertical scrolling:
http://manos.malihu.gr/animate-page-to-id-with-jquery
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
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
[...] Animate page to id with jquery [...]