Files
2026-09-21 15:32:12 +08:00

104 lines
2.0 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="demo">
<div ref="stageRef" class="stage">
<div
class="box"
v-draggable="config"
:style="{ left: `${x}px`, top: `${y}px` }"
v-text="'拖我'"
/>
</div>
<div class="bar">
<label class="check">
<input v-model="locked" type="checkbox" />
<span v-text="'锁定(interruptWhen 返回 true)'" />
</label>
</div>
<p class="state" v-text="status" />
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { VDraggable } from "timi-web";
import type { DraggableConfig } from "timi-web";
const x = ref(16);
const y = ref(16);
const locked = ref(false);
const status = ref("等待操作");
/**
* 指令只负责把移动过程报出来,位置由这里写回 style,
* 因此接不接管、怎么接管完全由业务决定。
*/
const config: DraggableConfig = {
onMouseDown: () => {
status.value = "已按下";
},
interruptWhen: () => locked.value,
onDragging: (_event, relX, relY) => {
x.value = Math.max(0, relX);
y.value = Math.max(0, relY);
status.value = `拖动中 relX=${relX} relY=${relY}`;
},
onDragged: (_event, offsetX, offsetY) => {
status.value = `已释放,本次位移 ${offsetX}, ${offsetY}`;
}
};
</script>
<style lang="less" scoped>
.demo {
.stage {
height: 14rem;
position: relative;
overflow: hidden;
margin-bottom: 1rem;
border: 1px dashed var(--vp-c-divider);
border-radius: .5rem;
background: var(--vp-c-bg-soft);
}
.box {
width: 5rem;
height: 5rem;
position: absolute;
display: flex;
cursor: grab;
font-size: .8125rem;
user-select: none;
align-items: center;
justify-content: center;
border-radius: .5rem;
color: #fff;
background: var(--vp-c-brand-1);
&:active {
cursor: grabbing;
}
}
.bar {
margin-bottom: .5rem;
}
.check {
display: flex;
gap: .375rem;
align-items: center;
font-size: .75rem;
color: var(--vp-c-text-2);
}
.state {
margin: 0;
font-size: .75rem;
font-family: monospace;
color: var(--vp-c-text-2);
}
}
</style>