触屏事件-上下左右滑动

window.touchMove=function(){
    // 用于纪录触摸开始时的坐标
    var startX=0,startY=0,
    //创建一个变量,用于保存触摸方向
    touchDirection="";
    //创建一个对象,用于保存滑动事件
    var funcs = {};
    if(arguments.length>=2&&arguments.length%2==0){
      for(var i=0;i<arguments.length;i+=2){
        funcs[arguments[i]]=arguments[i+1];
      }
      var elem =$("#wrapper")[0];
      var style = window.getComputedStyle ? window.getComputedStyle(elem,null) : null || elem.currentStyle;
      //主程序事件,用于给document绑定触摸事件
      document.addEventListener('touchstart',touchSatrtFunc, false);
      document.addEventListener('touchmove',touchMoveFunc, false);
      document.addEventListener('touchend', touchEndFunc, false);
      //定义触摸开始方法,获取触摸事件起始坐标
      function touchSatrtFunc(e){
        e.stopPropagation();
        touchDirection="";
        // e.preventDefault();
        var touch=e.touches[0];
        startX=touch.pageX;
        startY=touch.pageY;

        //惯性滑动的处理,让滑动变得流畅
        if(funcs.up!==undefined&&elem.offsetHeight>=document.body.clientHeight){
          startTopScroll = elem.scrollTop;
          //当滚动条在最顶部的时候
          if(startTopScroll <= 0)
              elem.scrollTop = 1;
          //当滚动条在最底部的时候
          if(startTopScroll + elem.offsetHeight >= elem.scrollHeight)
              elem.scrollTop = elem.scrollHeight - elem.offsetHeight - 1;
        }
        // else
        //   e.preventDefault();
      };
      //定义触摸移动时的方法,获取其坐标并调用判断触摸方向的方法
      function touchMoveFunc(e){
        e.stopPropagation();
        var touch = e.touches[0];
        //调用判断触摸方向的方法
        touchDirectionFunc(touch.pageX,touch.pageY,e);
      };
      //判断触摸方向的方法
      function touchDirectionFunc(x,y,e){
        var moveDirection =Math.abs(y-startY)/Math.abs(x-startX);
        if(y-startY<-10&&moveDirection>=1.5) {
          touchDirection="up";
        }
        else if(y-startY>10&&moveDirection>=1.5) {
          touchDirection="down";
        }
        else if(x-startX<-10&&moveDirection<=0.5) {
          touchDirection="left";
          e.preventDefault();
          // 横向滑动时阻止上下滑动
        }
        else if(x-startX>10&&moveDirection<=0.5) {
          touchDirection="right";
          e.preventDefault();
          // 横向滑动时阻止上下滑动
        }
        // else{
        //   e.preventDefault();
        // }
      };

      function touchEndFunc(e) {
        e.stopPropagation();
        //调用上拉事件
        if(touchDirection=="up"&&funcs.up!==undefined){
          funcs.up(e.target);
        }
        //调用下拉事件
        else if(touchDirection=="down"&&funcs.down!==undefined){
          funcs.down(e.target);
        }
        // 调用左滑事件
        else if(touchDirection=="left"&&funcs.left!==undefined){
          funcs.left(e.target);
        }
        //调用右滑事件
        else if(touchDirection=="right"&&funcs.right!==undefined){
          funcs.right(e.target);
        }
      }
    }
  }

  // 调用上拉,左滑,右滑方法
  touchMove("up",addMore,"left",moveLeft,"right",moveRight);

 

posted @ 2017-10-20 12:45  adouwt  阅读(202)  评论(0编辑  收藏  举报