使用javascript,jquery在移动设备上平滑滚动到页面的顶部或底部

时间:2017-02-15 16:59:50

标签: javascript mobile scroll smoothing

我有一个单页网站,在横向移动视图中分为两部分。所以我试图在页面的顶部或底部创建滚动/滑动效果。我试过这个检测代码

var bottomofDoc = $(".main_container").height();
$(window).scroll(function(){
        var cur_top = $(window).scrollTop();
        if(top < cur_top){
            direction = 'down';
            $('html,body').animate({scrollTop: $(document).height()}, 600);
            console.log(direction);
        }else{
            direction = 'up';
            $("html, body").animate({ scrollTop: 0 }, 600);
            console.log(direction);
        }
        top = cur_top;
    });

但它有问题,有闪烁,当我滚动一次它继续做多次动作。 任何想法如何平滑滚动到顶部或底部

1 个答案:

答案 0 :(得分:0)

$(function(){
     $('a[href*="#"]:not([href="#"])').click(function(){
          if(location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname){
               var target = $(this.hash);
               target = target.length ? target:$('[name=' + this.hash.slice(1) + ']');
               if(target.length){
                    $('html, body').animate({
                         scrollTop: target.offset().top
                    }, 1000);
                    return false;
               }
           }
     });
});
相关问题