Search code examples
jqueryanimationeffectssmooth-scrolling

Jquery Smoothscroll Function - How to Control Animation Speed?


Anyone can help me? Trying to Add a "Slow" function with my smoothscroll and control speed.

Hope to achievie a real "smoothscrolling".

Here are the codes:

$(document).ready(function(){
$('.smoothscroll').live('click',function(e){
    $('html,body').animate({
    'scrollTop': $($(this).attr('href')).offset().top+'px'

    });
    e.preventDefault(); 
});

});

Solution

  • Add the animation time as the 2nd parameter to the .animate() function (after the options object) like so:

    $(document).ready(function(){
        $('.smoothscroll').live('click',function(e){
            $('html,body').animate({
                'scrollTop': $($(this).attr('href')).offset().top+'px'
            }, 10000);
            e.preventDefault(); 
        });
    });
    

    In this example the animation will take 10,000 ms (10 seconds).