如何防止点击拖动?
How can I prevent click on drag?
我正在尝试让一个元素同时具有拖动和单击事件。我已经阅读并尝试了 event modifiers.
的组合
但是,无论我尝试什么,当拖动停止时,我都会得到一个点击。
请注意,在 MWE 中,这是在组件本身上完成的,但在我的实际应用中,我使用 .native
修饰符来拖动组件
如何在不点击的情况下拖动?
组件 Draggable
:
<template>
<div
@pointerdown="handleDown"
@pointerup="handleUp"
@pointercancel="handleUp"
@click="click = !click;"
:style="style"
ref="draggableRoot"
class="draggable"
>
drag me!<br />
am dragged? {{ drag }}<br />
am clicked? {{ click }}<br />
</div>
</template>
<script>
export default {
computed: {
style() {
return {
left: `${this.x}px`,
top: `${this.y}px`
};
}
},
data() {
return {
x: 100,
y: 100,
left: 0,
top: 0,
drag: false,
click: false
};
},
methods: {
handleMove({ pageX, pageY, clientX, clientY }) {
if (this.$refs.draggableRoot) {
this.x = pageX + this.left;
this.y = pageY + this.top;
this.drag = true;
}
},
handleDown(event) {
const { pageX, pageY } = event;
const { left, top } = this.$refs.draggableRoot.getBoundingClientRect();
this.left = left - pageX;
this.top = top - pageY;
document.addEventListener("pointermove", this.handleMove);
},
handleUp() {
document.removeEventListener("pointermove", this.handleMove);
this.drag = false;
}
}
};
</script>
<style scoped>
.draggable {
position: fixed;
border: solid coral 1px;
height: 100px;
}
</style>
也许这行得通:
在方法中添加setTimeout
handleUp
:
handleUp() {
document.removeEventListener("pointermove", this.handleMove);
setTimeout(() => this.drag = false) //this would move this assigment at the end of event queue
}
同时添加新方法 handleClick
并将其分配给事件 @click
:
handleClick() {
if(!this.drag) { //change click only if not draged
this.click = !this.click
}
}
我正在尝试让一个元素同时具有拖动和单击事件。我已经阅读并尝试了 event modifiers.
的组合但是,无论我尝试什么,当拖动停止时,我都会得到一个点击。
请注意,在 MWE 中,这是在组件本身上完成的,但在我的实际应用中,我使用 .native
修饰符来拖动组件
如何在不点击的情况下拖动?
组件 Draggable
:
<template>
<div
@pointerdown="handleDown"
@pointerup="handleUp"
@pointercancel="handleUp"
@click="click = !click;"
:style="style"
ref="draggableRoot"
class="draggable"
>
drag me!<br />
am dragged? {{ drag }}<br />
am clicked? {{ click }}<br />
</div>
</template>
<script>
export default {
computed: {
style() {
return {
left: `${this.x}px`,
top: `${this.y}px`
};
}
},
data() {
return {
x: 100,
y: 100,
left: 0,
top: 0,
drag: false,
click: false
};
},
methods: {
handleMove({ pageX, pageY, clientX, clientY }) {
if (this.$refs.draggableRoot) {
this.x = pageX + this.left;
this.y = pageY + this.top;
this.drag = true;
}
},
handleDown(event) {
const { pageX, pageY } = event;
const { left, top } = this.$refs.draggableRoot.getBoundingClientRect();
this.left = left - pageX;
this.top = top - pageY;
document.addEventListener("pointermove", this.handleMove);
},
handleUp() {
document.removeEventListener("pointermove", this.handleMove);
this.drag = false;
}
}
};
</script>
<style scoped>
.draggable {
position: fixed;
border: solid coral 1px;
height: 100px;
}
</style>
也许这行得通:
在方法中添加setTimeout
handleUp
:
handleUp() {
document.removeEventListener("pointermove", this.handleMove);
setTimeout(() => this.drag = false) //this would move this assigment at the end of event queue
}
同时添加新方法 handleClick
并将其分配给事件 @click
:
handleClick() {
if(!this.drag) { //change click only if not draged
this.click = !this.click
}
}