手势事件-手势封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>手势事件</title>
<style>
.box {
width: 320px;
height: 500px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box">
</div>
<script>
window.onload=function () {
var box=document.querySelector('.box');
var bindSwipEvent = function (dom,leftFn,rightFn) {
var start = null;
var move = null;
var moveEnd = null;
var moveBol = false;
dom.addEventListener('touchstart',function (e) {
start = e.touches[0].clientX;
})
dom.addEventListener('touchmove',function(e) {
moveEnd = e.touches[0].clientX;
move = moveEnd - start
moveBol = true;
})
dom.addEventListener('touchend',function(e) {
if (moveBol && Math.abs(move)>50) {
if (move > 0) {
rightFn && rightFn.call(this.e)
} else {
leftFn && leftFn.call(this,e)
}
}
})
start = 0;
move = 0;
moveEnd = 0;
moveBol = false;
}
bindSwipeEvent(box,function (e) {
console.log("左移"+this+e);
},function (e) {
console.log("右移"+this+e);
});
}
</script>
</body>
</html>