How to scroll the window using JQuery $.scrollTo() function

Matt Doak picture Matt Doak · May 7, 2009 · Viewed 261.6k times · Source

I'm trying to scroll down 100px every time the user gets near the top of the document.

I have the function executing when the user gets close to the top of the document, but the .scrollTo function isn't working.

I put an alert after and before to check to see if it actually was the line or not that was stopping it and only the first alert goes off, here's the code:

alert("starting");
$.scrollTo({ top: '+=100px', left: '+=0px' }, 800);
alert("finished");

I know I have the jquery page linked properly because I'm using many other jquery functions throughout and they all work fine. I've also tried removing the 'px' from above and it doesn't seem to make a difference.

Answer

Mihai picture Mihai · Feb 18, 2011
$('html, body').animate({scrollTop: $("#page").offset().top}, 2000);