这是我做react项目的时候写的,其他框架稍作改动即可。
<button onTouchStart= {this.handelTouchStart.bind(this)}
onTouchMove={this.handelTouchMove.bind(this)}
onTouchEnd={this.handelTouchEnd.bind(this)}>
</button>
handelTouchStart(e) {
const { target } = e;
const { touches: [touch] } = e;
target.touchX = touch.clientX;
target.touchY = touch.clientY;
target.touchT = Date.now();
target.longTapTick = setTimeout(() => {
console.log('长按了。。。');
}, 3000);
}
handelTouchMove(e) {
const { target } = e;
const { touches: [touch] } = e;
const { clientX: x, clientY: y } = touch;
if (Math.abs(y - target.touchY) > 10 || Math.abs(x - target.touchX) > 10) {
clearTimeout(target.longTapTick);
}
}
handelTouchEnd(e) {
const { target } = e;
clearTimeout(target.longTapTick);
}
网友评论