Scroll window smoothly in jQuery - Animated scroll
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
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.
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?
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
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.
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!
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.
I used the javascript fix, and it worked like a charm. thank you so much!!!!
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.
I tested it on IE 8 (Windows XP) and IE 8 (Windows 7). It works fine.
(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.
// -- 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;} );
}
}
I think i missed that out ;)
I'm probably doing it wrong, as I'm a complete n00b, but any advice would be much appreciated.
Thanks,
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.
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);
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
Helped me a lot.
window.scrollBy(x,y);
// X for horizontal and Y for vertical.
So, you can scroll window in both ways.
Thanks for this simple, usefull, and reusable tip
have a nice day


