简体   繁体   English

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

[英]Smooth scroll to top or bottom of page with javascript,jquery on mobile

I have a one page website which is divided into two parts on horizontal mobile view. 我有一个单页网站,在水平移动视图上分为两个部分。 So am trying to create a scroll/swipe effect to top or bottom of the page. 因此,我尝试在页面顶部或底部创建滚动/滑动效果。 I tried this code of detection 我尝试了此检测代码

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

But it works buggy,with flickering and when i scroll once it continue doing actions many times. 但是它的工作原理是越野车,带有闪烁,当我滚动一次时,它将继续执行很多次操作。 Any ideas how to achive smooth scrolling to top or bottom 任何想法如何实现平滑滚动到顶部或底部

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM