9 changed files with 112 additions and 9 deletions
@ -0,0 +1,47 @@ |
|||||||
|
// 长按弹出上拉操作面板
|
||||||
|
class Mobile { |
||||||
|
toucheX: number = 0; |
||||||
|
toucheY: number = 0; |
||||||
|
timeOutEvent: number = 0; |
||||||
|
data?: any; |
||||||
|
onLongTouch?: Function; |
||||||
|
onClick?: Function; |
||||||
|
e: any; |
||||||
|
|
||||||
|
onTouchStart(e, data) { |
||||||
|
this.toucheX = e.targetTouches[0].screenX; |
||||||
|
this.toucheY = e.targetTouches[0].screenY; |
||||||
|
this.data = data; |
||||||
|
this.e = e; |
||||||
|
|
||||||
|
// 开启定时器前先清除定时器,防止重复触发
|
||||||
|
this.timeOutEvent && clearTimeout(this.timeOutEvent); |
||||||
|
// 显示上拉面板
|
||||||
|
this.timeOutEvent = setTimeout(() => { |
||||||
|
this.onLongTouch && this.onLongTouch(e, data); |
||||||
|
this.timeOutEvent = 0; |
||||||
|
}, 1000); |
||||||
|
e.preventDefault(); // 阻止系统默认事件
|
||||||
|
} |
||||||
|
onTouchMove(e) { |
||||||
|
const moveX = e.targetTouches[0].screenX; |
||||||
|
const moveY = e.targetTouches[0].screenY; |
||||||
|
// 解决vivo机型,手指没有move,touchmove事件仍然会调用而导致setTimeout被clear
|
||||||
|
if (this.toucheX !== moveX || this.toucheY !== moveY) { |
||||||
|
// 手指滑动,清除定时器,中断长按逻辑
|
||||||
|
this.timeOutEvent && clearTimeout(this.timeOutEvent); |
||||||
|
} |
||||||
|
} |
||||||
|
onTouchEnd() { |
||||||
|
// 清除定时器,结束长按逻辑
|
||||||
|
this.timeOutEvent && clearTimeout(this.timeOutEvent); |
||||||
|
// 若手指离开屏幕,时间小于我们设置的长按时间,则为点击事件
|
||||||
|
if (this.timeOutEvent !== 0) { |
||||||
|
this.onClick && this.onClick(this.e, this.data); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const userMobile = () => { |
||||||
|
return new Mobile(); |
||||||
|
}; |
Loading…
Reference in new issue