Jquery Smoothscroll Function - How to Control Animation Speed?

自古美人都是妖i 提交于 2020-01-04 05:35:27

问题


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

});

回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/11092794/jquery-smoothscroll-function-how-to-control-animation-speed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!