重点关注isClick方法。
startDrag(event) {
console.log('startDrag', event)
// event.preventDefault(); // 阻止默认行为
this.dragging = true;
this.startTime = event.timeStamp;
// 记录初始触摸位置
this.initialX = event.touches[0].clientX;
this.initialY = event.touches[0].clientY;
// 记录当前偏移量
this.offsetX = parseInt(this.buttonStyle.right.slice(0, -2));
this.offsetY = parseInt(this.buttonStyle.top.slice(0, -2));
},
onDrag(event) {
event.preventDefault(); // 阻止默认行为
if (this.dragging) {
// 计算偏移量
const deltaX = event.touches[0].clientX - this.initialX;
const deltaY = event.touches[0].clientY - this.initialY;
// 更新按钮位置
if (this.offsetX - deltaX < 0) {
this.buttonStyle.right = `0px`;
} else if (this.offsetX - deltaX > window.innerWidth - 60) {
this.buttonStyle.right = `${window.innerWidth - 60}px`;
} else {
this.buttonStyle.right = `${this.offsetX - deltaX}px`;
}
if (this.offsetY + deltaY < 80) {
this.buttonStyle.top = `80px`;
} else if (this.offsetY + deltaY > window.innerHeight - 80) {
this.buttonStyle.top = `${window.innerHeight - 80}px`;
} else {
this.buttonStyle.top = `${this.offsetY + deltaY}px`;
}
}
},
endDrag(event) {
// event.stopPropagation();
console.log(this.isClick(event))
if (this.isClick(event)) {
// 如果是点击操作,则触发wx-open-launch-weapp组件的事件
this.$emit('launch');
} else {
// 如果是拖拽操作,则执行拖拽相关的逻辑
this.dragging = false;
}
},
isClick(event) {
console.log('isClick', event)
// 获取touchstart事件的位置和时间戳
const startX = this.initialX;
const startY = this.initialY;
const startTime = this.startTime;
// 获取touchend事件的位置和时间戳
const endX = event.changedTouches[0].clientX;
const endY = event.changedTouches[0].clientY;
const endTime = event.timeStamp;
// 计算位置差和时间差
const deltaX = Math.abs(endX - startX);
const deltaY = Math.abs(endY - startY);
const deltaTime = endTime - startTime;
// 判断是否是点击操作
if (deltaX < 10 && deltaY < 10 && deltaTime < 250) {
return true; // 点击操作
} else {
return false; // 非点击操作
}
},
网友评论