1.触屏事件
①.触屏(touch)事件
touchstart 手指触摸屏幕时触发,即使已经有手指在屏幕上也会触发
touchmove 手指在屏幕滑动时触发
touchend 手指从屏幕时移开时触发
touchcancel 当触控点被特定的实现方式打乱时触发(例如,弹框),一般不用
②.事件绑定方式,DOM0和DOM2都可以,推荐使用DOM2方式
box.addEventListener(‘touchstart’,function () {
console.log(‘touchstart’);
});
③.移动端响应Mouch事件
移动端借助touch去触发mouse事件。
在手机上,当我们手触碰屏幕时,要过 300ms 左右才会触发 mouse 事件。
例1:给box绑定mousemove、mousedown、mouseup 、click
在移动端点击会触发mousemove、mousedown、mouseup 、click事件。
直接滑动什么都不触发。
长按会触发mousemove事件。
例2:给box绑定touchstart、 touchend 、click
点击会触发touchstart、 touchend 、click事件。
长按会触发touchstart、 touchend事件。
2. touchEvent
①.touchEvent对象
changedTouches 在元素上改变状态的手指个数,例如:手指按上或者离开。返回touchList对象。
targetTouches 触发事件时在元素上的手指个数,返回touchList对象
touches 触发事件时在屏幕上的手指个数,返回touchList对象
②.touchList对象
touchList类数组对象中有Touch对象和length,length表示手指在屏幕上的数量。
给box监听touchstart事件,changedTouches,targetTouches,touches的length都为1。
给box监听touchend事件,changedTouches的length为1,targetTouches,touches的length为0。
一般情况下:
touchend事件,获取离开的触点,用changedTouches。
touchmove和touchstart, targetTouches和changedTouches都可以用。
③.Touch对象
表示一个触点
clientX 触点在视口中的位置
clientY
force 按压力度