用简化动画替换滚动

时间:2013-10-14 06:13:50

标签: javascript jquery css animation

我一直在努力用漂亮的缓动动画替换默认的$(window).scroll()行为。但到目前为止没有运气。以下是我的代码。

$(window).scroll(function(e){
    e.preventDefault();

    var cp=$(window).scrollTop(); // current scroll position

    //animate with custom easing
    $('html, body').animate({scrollTop:cp}, 50,'easeInElastic');

});

但这不起作用。它只是滚动1-3个像素而且非常慢。

如果我将其更改为$('html, body').animate({scrollTop:500}, 50,'easeInElastic'); 它只做了一次而那不是我想要的。

当用户按下滚动条按钮时,我希望用简化动画替换标准滚动行为。

JSFiddle链接http://jsfiddle.net/bfDrp/

2 个答案:

答案 0 :(得分:0)

看看这个插件:

http://manos.malihu.gr/jquery-custom-content-scroller/

这是实施:

<script>
(function($){
    $(document).ready(function(){
        $("body").mCustomScrollbar();
    });
})(jQuery);

答案 1 :(得分:0)

  

在你也可以使用scrollX的地方使用.scrollTo函数   并用窗口滚动。

window.scrollTo(0,150,500);