UNPKG

touch2mouse

Version:

可以将指定的dom元素触摸操作转换为鼠标操作信号

63 lines (54 loc) 1.78 kB
## 说明 移动端touch转鼠标操作交互封装模拟。可以将指定dom对象的触摸操作直接转换成鼠标操作信号 #### 第一步 实例化交互逻辑 <div class="touch-area" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd" @touchcancel="handleTouchCancel" @mousemove="handleMouseMove" @mousedown="handleMouseDown" @mouseup="handleMouseUp"> </div> ```js mounted() { // 初始化触摸事件处理器 this.touchEventHandler = new TouchEventHandler({ minMovementThreshold: 3, // 最小移动阈值(px) minScaleThreshold: 5, // 最小缩放阈值(px) scaleBaseDelta: 500, // 缩放基础增量(px) scaleIntensityMultiplier: 1, // 缩放强度乘数 holdThreshold: 500, // 长按时间阈值(ms) holdPositionThreshold: 30 // 长按位置阈值(px) }); // 设置目标元素 const touchArea = this.$el.querySelector('.touch-area'); if (touchArea) { this.touchEventHandler.setTargetElement(touchArea); } } beforeUnmount() { // 清理资源 if (this.touchEventHandler) { this.touchEventHandler.destroy(); } }, ``` ---- #### 第二步 绑定事件(TouchEventHandler将会自动触发mousedown、mouseup、mousemove、wheel事件) ```js handleTouchStart(event) { this.touchEventHandler.handleTouchStart(event); }, handleTouchMove(event) { this.touchEventHandler.handleTouchMove(event); }, handleTouchEnd(event) { this.touchEventHandler.handleTouchEnd(event); }, handleTouchCancel(event) { this.touchEventHandler.handleTouchCancel(event); } ``` ----