关于移动端事件的一些笔记
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
// 1.声明变量
var startX, startY, endX, endY, absX, absY;
// 2.给document添加touchstart事件
document.addEventListener("touchstart", function (e) {
var touches = e.targetTouches[0];
startX = touches.clientX;
startY = touches.clientY;
// console.log(startX, startY);
})
// 3. 给document添加touchend事件
document.addEventListener("touchend", function (e) {
var touches = e.changedTouches[0];
endX = touches.clientX;
endY = touches.clientY;
// 4. 计算坐标差值的绝对值
absX = Math.abs(endX - startX);
absY = Math.abs(endY - startY);
if (absX == absY) {
return;
}
// 5. 判断是水平还是垂直
// 6. 如果是水平 判断左右
// 7.如果是垂直 判断上下
var direction = absX > absY ?
endX - startX > 0 ? "向右" : "向左" :
endY - startY > 0 ? "向下" : "向上";
console.log(direction);
})
</script>
</body>
</html>