我正在尝试同时具有拖动和单击事件的元素。我已阅读并尝试过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>
答案 0 :(得分:1)
也许可行:
在方法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
}
}