views:

806

answers:

3

Page must be scrolled after clicking on link with id (let it be #link).

Here is my code, which doesnt work:

$(document).ready(function(){
    $("#link").click(function () { 
     $(this).animate({ scrollTop: 2000 }, 'slow'); 
    });
});

But this one works, after the page loads it slowly scrolls to the bottom of the page:

$(document).ready(function() {
    $('html, body').animate({ scrollTop: 6000 }, 'slow'); 
});

Height of the body is 6000px.

+2  A: 

It looks like your trying to animate 'this' which would be the link!

You should try $(window).animate instead! or maybe even document, sorry not sure!

Rigobert Song
thanks, but "$(window).animate" doesnt work too
Mike
+1  A: 

In your first example, You're setting the scrollTop of the actual link element which has nothing within it to scroll. ( no overflow )

$(document).ready(function(){
    $("#link").click(function () { 
        $("body").animate({ scrollTop: 2000 }, 'slow'); 
    });
});

Hers's a pretty good explanation of https://developer.mozilla.org/En/DOM/Element.scrollTop

Chad Grant
works fine with scroll from top to the bottom.
Mike
but if link is placed in the bottom (for example, from 4000px of the top corner) and I want to scroll it to the top, it scrolls from "top-to-the-point" (page throws to the top and then scrolls down), but needs to scroll from "bottom-to-the-top-point" (from existing point, without throwing). Pleeeease help me to fix that
Mike
can you answer?
Mike
thanks anyway..
Mike
if you want it to animate to the top from the bottom { scrollTop: 0 }
Chad Grant
+1  A: 

try $('html,body').animate to support all browsers

Karthik