vue3中使用Vuedraggable实现拖拽排序

直接上效果图

1.实现原理:使用 了Vuedraggable

Vuedraggable是一个专为 Vue 打造的拖拽排序模块,基于 Sortablejs 封装,支持 Vue3 或 Vue2

2.安装:pnpm i -S vuedraggable@next

vue3一定要安装最新版的vuedraggable

3.代码(效果图是项目中的,代码是自己写的demo,效果都一样)

<template>
    <div class="drag-box">
        <Draggable :list="list" :animation="500" class="list-group" :forceFallback="true" chosen-class="chosenClass"
            @end="endAble">
            <template #item="{ element }">
                <div class="items" @click="clickDetail(element)">
                    <div class="title">{{ element.label }}</div>
                    <img :src="element.image" alt="" style="width: 100px;">
                </div>
            </template>
        </Draggable>
    </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import Draggable from "vuedraggable";

const list = reactive([
    {
        label: "模块1",
        id: 1,
        image: 'https://lupic.cdn.bcebos.com/20210629/3000005161_14.jpg'
    },{
        label: "模块2",
        id: 2,
        image: 'https://lupic.cdn.bcebos.com/20210629/3000005161_14.jpg'
    },{
        label: "模块3",
        id: 3,
        image: 'https://lupic.cdn.bcebos.com/20210629/3000005161_14.jpg'
    }]);
function endAble(e) {
    console.log(e);
}
function clickDetail(item) {
    console.log(item, '点击我');
}
</script>

<style scoped>
.list-group {
    width: 70%;
    display: flex;
    flex-wrap: wrap;
    padding: 20px;
}

.items {
    width: 200px;
    height: 200px;
    padding: 20px;
    background: #e3e3e3;
    border-radius: 8px;
    box-sizing: border-box;
    margin-right: 20px;
    margin-bottom: 20px;
}

</style>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值