移动端判断手指向上滑动还是向下滑动
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;
}
//滑动处理
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);