且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javascript - 如何用原生JS&&jQuery&&Vue.js判断手机的滑动?

更新时间:2022-10-30 13:24:46

已使用touchstart,touchend解决

(function(){
    var currentPage = 0;
    var direction = function(up, down){
        var YStack = [];
        document.body.addEventListener('touchstart',function(e){
            YStack.length = 0;
            YStack.push(e.touches[0].clientY);
        },false);
        document.body.addEventListener('touchend',function(e){
           YStack.push(e.changedTouches[0].clientY);
           YStack[1]-YStack[0] >= 100?down():void 0;
           YStack[1]-YStack[0] <= -100?up():void 0;
        },false);
    }
    var scrollUp = function(){
       if(currentPage<=3){
           currentPage++;
           $('div#wrapper').css({'top':-currentPage+'00%'});
       }
    }
    var scrollDown = function(){
      if(currentPage>=1){
          currentPage--;
          $('div#wrapper').css({'top':-currentPage+'00%'});
      }
    }
    direction(scrollUp,scrollDown);
})();