Niccolo
Niccolo

Reputation: 157

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(); 
});

});

Upvotes: 2

Views: 2789

Answers (2)

Mohamed Salem Lamiri
Mohamed Salem Lamiri

Reputation: 6077

Thanks for the answer nbsp!

Just to update..

jQuery .live() has been removed in version 1.9 onwards.

so here what worked for me :

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

Upvotes: 0

user99874
user99874

Reputation:

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).

Upvotes: 1

Related Questions