03.26.2009

Scroll window smoothly in jQuery - Animated scroll

Bookmark and Share
Add to DZone


Using jQuery animate() function to do a smooth scrolling:

I have also created a div as follow:

<div id="scrollToHere">
Scroll to here
</div>

You need something to run your script. Create a button like this:

<input type="button" onclick="scollWin();" value="Scroll up" />

The jQuery Code will be like this:

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

Here is something to try:

now jQuery window scrolling is done with animation.

Enjoy it..

39 comments

Comment from: gunsan [Visitor] · http://gunadiw.wordpress.com
thanks, it's really cool.
04/30/09 @ 11:59
Comment from: EllisGL [Visitor]
It's smooth for the most part. I do get a 2 "clicks" the first usage.
05/12/09 @ 15:12
Comment from: kristoph [Visitor]
Doesn't work in Opera
05/13/09 @ 09:52
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
The down scroll is working fine. But the UP scroll is not. It jumps up without animation.
All you have to do is remove the (,body) from $("html, body")

Because Opera supports both. So, it scrolls twice.
Let the code be: $("html").animate.....

On safari, firefox and IEs: this code works fine.
05/13/09 @ 11:57
Comment from: Paul Radich [Visitor] · http://www.codetricky.com
Nice Thanks!
06/14/09 @ 07:02
Comment from: ZK@Web Marketing Blog [Visitor] · http://www.webtrafficroi.com


Just started using jQuery here a few months ago, love it! Can’t wait to read more from this blog. Know of any good jQuery Blog Aggregators?
06/21/09 @ 06:46
Cool, thanks!
06/25/09 @ 06:59
Comment from: Tyler [Visitor]
Thanks! Just wanted to point out a small typo, the onclick should of course read "scrollWin();" but an r was left out. A very small error, but it does break the script, so wanted to point it out in case anyone was having trouble.
06/30/09 @ 04:49
Comment from: shick [Visitor]
Thanks for the help, i've been looking everywhere for an explanation on the ability to scroll smoothly to an item in jquery, but this is the first that has worked for me.

however, when i attempt to use the function with a link as opposed to a button, i get a screen flash in firefox and ie and chrome. here's the link vs the button.

code
*a href="#" onclick="if($.browser.opera){$('html').animate({scrollTop: $('#header').offset().top}, 2000);} else $('html,body').animate({scrollTop: $('#header').offset().top}, 2000);">TOP JQ*/a*

*input type="button" style="border: 1px solid rgb(51, 51, 51); background-color: rgb(102, 102, 102); color: rgb(255, 255, 255);" value="Click here to scroll up" onclick="if($.browser.opera){$('html').animate({scrollTop: $('#header').offset().top}, 2000);} else $('html,body').animate({scrollTop: $('#header').offset().top}, 2000);"/*

please let me know if you see anything that may be causing this, or if my code has some sort of error in it. this is my first real attempt at jquery and js, so any help would be awesome. the * represent the tags, it wouldnt let me post otherwise

Thanks
08/03/09 @ 20:53
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
Thanks Shick for your comment.

This is the first time I hear about flashing screen when trying this code.

Actually,
This code was tested on The following browsers:
- IE 6 and 7 (window)
- Firefox (windows and mac)
- Opera (windows and mac)
- Safari (mac)

I did not experience any problem with this code.
Please provide with all information about the jQuery version and your browser version.
08/04/09 @ 09:34
Comment from: Shick [Visitor]
i'm using jquery version 1.3.2.

Browser Versions
Firefox 3.0.12
IE 6, 7, 8
Chrome is on the latest verison, i'm on a different computer.

thanks for the reply, and like i said, it's only when i use the code as a link. when i use it as a button, like you have it, it doesn't flash, even when both link to the header.

Should I be using it "onclick" like the button when i'm using it on the link?

thanks again!
08/05/09 @ 07:44
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
I think i've got the problem..

If you put # in the HREF attribute of the A tag, then the window will go up on click to go the ID (which is nothing)

To fix this, Please try to use the following:
- Put "javascript:;" in the HREF attribute of the A tag.
- Or use DIV, SPAN... etc with cursor:pointer css style and onclick event.

This will fix it.
08/05/09 @ 09:33
Comment from: Shick [Visitor]
Thank you so much!!!

I used the javascript fix, and it worked like a charm. thank you so much!!!!
08/06/09 @ 00:55
Comment from: Anz [Visitor] · http://www.clientsideasp.net/
Click 2 or 3 times in the scroll link and try using your mouse scroll wheel!!
08/16/09 @ 16:12
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
The scroll animation keeps going. So, if you try to scroll the window using mouse wheel after clicking on the button, it will surely continue till the animation is done.

If you want to stop the animation in any action taken, then try to use $("html,body").stop();
this will stop all animations on window.
08/17/09 @ 09:30
Comment from: Evan [Visitor]
is there a simple fix for this to scroll horizontally, to the left?
08/20/09 @ 04:17
Comment from: steven [Visitor] · http://www.sigmawidgets.com
Not working on ie8
10/18/09 @ 07:07
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
Actually, It works fine on IE8

I tested it on IE 8 (Windows XP) and IE 8 (Windows 7). It works fine.
10/18/09 @ 09:51
Comment from: toto [Visitor] · http://none00.tumblr.com
it does work in opera. this is lame such a function wasn't included inside jquery itself, it is so basic.
(prototype does it out of the box). saying this 'cause jquery end up with lame plugins that do zillions more stuff more than just that, wich in a way sucks.
11/02/09 @ 21:14
Comment from: ben [Visitor] · http://none00.tumblr.com
so yeah thanks a lot.
11/02/09 @ 21:15
Comment from: phil [Visitor] · http://pupco.net
mispelling: "scollWin();"
11/13/09 @ 19:26
Comment from: Hiperblade [Visitor] Email · http://cronacheartificiali.blogspot.com/
// nice work

// -- only a suggestion
// any click add animation :
// many click => many animations
// You can add a flag (and a call back) to disable queuing of animations
// and/or add a position check to prevent animations unwanted

var scrolling = false;

function scrollWin(){
if(scrolling == false){
scrolling = true;
$('html,body').animate({scrollTop: $('#scrollToHere').offset().top}, 2000, "linear", function(){scrolling = false;} );
}
}
11/16/09 @ 23:59
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
Thanks for the suggestion Hiperblade.

I think i missed that out ;)
11/17/09 @ 00:30
Comment from: Chris Brock [Visitor] Email
This is great, but only one problem. I can't work out how to get it to work within a DIV - ie. I have an overflow DIV on a static page, and I just want to scroll within the DIV.

I'm probably doing it wrong, as I'm a complete n00b, but any advice would be much appreciated.

Thanks,

Chris

11/20/09 @ 22:13
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
Well Chris,

In this case you should work with the following CSS properties to get it work:
- TOP

If you are using a DIV inside your div (overflowed), This means your code will be something like this:

<div id="overflow"> // Ths should has CSS: position: relative;
<div id="scroller"></div> // This must has CSS: position: absolute;
</div>

then you can use the jQuery code:
$("#scroller").animate({'top','100px'},400); // For scrolling the div down.
$("#scroller").animate({'top','0px'},400); // For scrolling the div up.

This means that you will scroll the DIV (id="scroller") within the DIV (id="main")

Good luck Chris.
11/21/09 @ 02:19
Comment from: M. [Visitor] Email
When the site is scrolled to the top, the button "Scroll up" works by scrolling down... FF3.5
12/01/09 @ 23:40
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
Actually,

When you scroll it (I mean by clicking one of the buttons), It goes to a location.. No matter where the location is.
You may use a calculation instead of #id.
something like this:

// Scrolling Down
$('body').animate({
scrollTop: '-=300px'
}, 2000);

// Scrolling Up
$('body').animate({
scrollTop: '+=300px'
}, 2000);
12/02/09 @ 00:02
Comment from: 3m masr [Visitor] · http://theadhamian.wordpress.com
thanks for great article
i have a series of tutorials about form's validation using JQuery , i will be happy to read your comments
http://bit.ly/8xSB7F
http://bit.ly/8W2Vmf
12/02/09 @ 13:30
Comment from: El Oscuro [Visitor] · http://none.yet
Amazing article, thank you!

Helped me a lot.
12/02/09 @ 16:41
Comment from: Martin [Visitor] · http://www.martinsoft.sk
Works fine. Thank you for helpfully article.
12/08/09 @ 00:37
Comment from: Natalie [Visitor] · http://nataliearoberts.com
Is there any way this can be used horizontally? Either to a div or using pixel locations?
12/13/09 @ 02:27
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
Actually, there a Javascript code for that

window.scrollBy(x,y);
// X for horizontal and Y for vertical.

So, you can scroll window in both ways.
12/13/09 @ 10:09
Have this type of action working great on above mentioned page...but...the links are in the menu. Soooo...if I'm NOT on the conditions page AND click a link that would normally scroll great IF I was on the conditions page scrolls all content above it up under the menu and header. Any idea how to fix this?
12/29/09 @ 02:58
Comment from: Andres [Visitor] · http://www.mauriciocastillo.com
Works fine, tks
12/29/09 @ 07:37
Comment from: 奇流网 [Visitor] · http://www.qeenoo.com
is very good
01/12/10 @ 13:23
Comment from: Syd [Visitor] · http://www.divorcexplained.com
Good one.
01/23/10 @ 23:21
Comment from: Preetham [Visitor] · http://preetham.in
simple and efficient,, Thanks
02/09/10 @ 19:19
Comment from: Robin [Visitor] Email · http://fretless.in
Thanks, Was really helpful!
02/10/10 @ 04:38
Comment from: poupouille [Visitor]
hi

Thanks for this simple, usefull, and reusable tip

have a nice day
03/03/10 @ 11:18

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Free Blog Themes / Templates