移動端判斷手指向上滑動仍是向下滑動

function GetSlideDirection(startX, startY, endX, endY) {  
      var dy = startY - endY;  
      //var dx = endX - startX;  
      var result = 0;
      if(dy>0) {//向上滑動
              result=1;
      }else{//向下滑動
              result=2;
      }

      return result;  
  }  ide

//滑動處理  
  var startX, startY;  
  document.addEventListener('touchstart',function (ev) {  
      startX = ev.touches[0].pageX;  
      startY = ev.touches[0].pageY;    
  }, false);  

  document.addEventListener('touchend',function (ev) {  
      var endX, endY;  
      endX = ev.changedTouches[0].pageX;  
      endY = ev.changedTouches[0].pageY;  
      var direction = GetSlideDirection(startX, startY, endX, endY);
      switch(direction) {  
          case 0:  
              break;  
          case 1:  
              // 向上
              alert("up");
              break;  
          case 2:  
              // 向下
             alert("down");
              break;  
        
          default:             
      }  
  }, false);  it

相關文章
相關標籤/搜索