基础使用实例
<template>
<!-- 点击按钮开始拖动 -->
<button @click="start">start</button>
<!-- 点击按钮暂停拖动 -->
<button @click="pause">pause</button>
<!-- 点击按钮禁用拖动 -->
<button @click="disabled = true">disabled</button>
<div class="flex">
<!-- 使用VueDraggable组件实现拖拽功能,设置相关属性 -->
<VueDraggable ref="VueDraggableRed" v-model="list" :disabled="disabled" :animation="150" ghostClass="ghost"
class="flex flex-col gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded" @start="onStart"
@update="onUpdate">
<!-- 循环渲染拖拽元素 -->
<div v-for="item in list" :key="item.id" class="cursor-move h-30 bg-gray-500/5 rounded p-3 cursor-move">
{{ item.name }}
</div>
</VueDraggable>
<!-- 预览列表组件 -->
<preview-list :list="list" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { type UseDraggableReturn, VueDraggable } from 'vue-draggable-plus'
const VueDraggableRed = ref<UseDraggableReturn>()
const disabled = ref<boolean>(false)
const list = ref([
{
name: 'Joao',
id: 1
},
{
name: 'Jean',
id: 2
},
{
name: 'Johanna',
id: 3
},
{
name: 'Juan',
id: 4
}
])
// 暂停拖动操作
const pause = () => {
VueDraggableRed.value?.pause()
}
// 开始拖动操作
const start = () => {
VueDraggableRed.value?.start()
}
// 拖动开始事件
const onStart = () => {
console.log('start')
}
// 拖动更新事件
const onUpdate = () => {
console.log('update')
}
</script>
<style scoped lang="scss">
.ghost {
opacity: 0.5;
background: #c8ebfb;
}
</style>
其他细节可去官网查看文档