vuedraggable實(shí)現(xiàn)拖拽功能
本文實(shí)例為大家分享了vuedraggable實(shí)現(xiàn)拖拽功能的具體代碼,供大家參考,具體內(nèi)容如下

項(xiàng)目需求
簡單實(shí)現(xiàn)一個vue拖拽小案例,右側(cè)選項(xiàng)區(qū)拖拽到左側(cè)目標(biāo)區(qū)域,拖動成功的不能再次拖動,并改變顏色。
安裝 vuedraggable 插件
cnpm i -S vuedraggable
頁面直接引用
import draggable from “vuedraggable”;
…
components: { draggable },源碼參考
<template>
? <div class="container">
? ? <div class="content">
? ? ? <div class="targetContent">
? ? ? ? <draggable
? ? ? ? ? :list="resultlists"
? ? ? ? ? group="article"
? ? ? ? ? :disabled="disabled"
? ? ? ? ? @start="targetStart"
? ? ? ? ? @end="targetEnd"
? ? ? ? ? class="targetCard"
? ? ? ? ? style="height: 100px"
? ? ? ? >
? ? ? ? ? <div
? ? ? ? ? ? v-for="resultlists in resultlists"
? ? ? ? ? ? :key="resultlists.id"
? ? ? ? ? ? class="list-complete-item"
? ? ? ? ? >
? ? ? ? ? ? <div class="list-complete-item-handle">{{ resultlists.name }}</div>
? ? ? ? ? </div>
? ? ? ? </draggable>
? ? ? </div>
? ? ? <div class="optionsContent">
? ? ? ? <draggable
? ? ? ? ? :list="targetLists"
? ? ? ? ? :options="{
? ? ? ? ? ? group: { name: optionsName, pull: 'clone' },
? ? ? ? ? ? sort: false,
? ? ? ? ? }"
? ? ? ? ? filter=".undraggable"
? ? ? ? ? @end="optionEnd"
? ? ? ? ? class="dragArea"
? ? ? ? >
? ? ? ? ? <div
? ? ? ? ? ? v-for="targetList in targetLists"
? ? ? ? ? ? :key="targetList.id"
? ? ? ? ? ? :class="{ undraggable: targetList.flag }"
? ? ? ? ? ? class="list-complete-item"
? ? ? ? ? >
? ? ? ? ? ? <div class="list-complete-item-handle2">{{ targetList.name }}</div>
? ? ? ? ? </div>
? ? ? ? </draggable>
? ? ? </div>
? ? </div>
? </div>
</template>
<script>
import draggable from "vuedraggable";
export default {
? name: "DndList",
? components: { draggable },
? watch: {},
? data() {
? ? return {
? ? ? optionsName: "article",
? ? ? disabled: false,
? ? ? resultlists: [],
? ? ? targetLists: [
? ? ? ? { id: 1, name: "平板支撐" },
? ? ? ? { id: 2, name: "俄挺" },
? ? ? ? { id: 3, name: "空中自行車" },
? ? ? ? { id: 4, name: "波比" },
? ? ? ? { id: 5, name: "兩頭起摸腳" },
? ? ? ? { id: 6, name: "窄距俯臥撐" },
? ? ? ? { id: 7, name: "寬距俯臥撐" },
? ? ? ? { id: 8, name: "鉆石俯臥撐" },
? ? ? ? { id: 9, name: "空中擊掌俯臥稱" },
? ? ? ? { id: 10, name: "深蹲提膝跳" },
? ? ? ],
? ? };
? },
? computed: {},
? methods: {
? ?? ?// 右側(cè)拖拽結(jié)束事件
? ? optionEnd(ev) {
? ? // 如果拖拽目標(biāo)區(qū)域和左側(cè)區(qū)域的 name 一致,允許拖拽,并改變狀態(tài)
? ? ? if (ev.to.className === "targetCard") {
? ? ? ? this.$set(this.targetLists[ev.oldIndex], "flag", true);
? ? ? }
? ? },
?? ?// 左側(cè)區(qū)域開始拖動事件(修改右側(cè)區(qū)域的名字,不一致的話拖動失敗)
? ? targetStart() {
? ? ? this.optionsName = "1111";
? ? },
?? ?// 拖動結(jié)束時,把右側(cè)區(qū)域的名字恢復(fù)
? ? targetEnd() {
? ? ? this.optionsName = "article";
? ? },
? },
};
</script>
<style lang="less" scoped>
.container {
? width: 100%;
? height: 100%;
? background-color: rgba(5, 35, 56, 0.6);
? display: flex;
? align-items: center;
? justify-content: center;
? .content {
? ? width: 1200px;
? ? height: 600px;
? ? background-color: #052338;
? ? // margin: 120px auto auto;
? ? display: flex;
? ? .targetContent {
? ? ? width: 100%;
? ? ? height: 100%;
? ? }
? ? .optionsContent {
? ? ? width: 100%;
? ? ? height: 100%;
? ? ? border-left: 2px solid #4ab7bd;
? ? }
? }
}
.list-complete-item {
? display: inline-block;
? font-size: 16px;
? font-weight: 400;
? color: #ffffff;
? padding: 10px 18px;
? margin: 10px 8px 0;
? background: rgba(237, 245, 255, 0.1);
? border: 1px solid #bfcbd9;
? border-radius: 0.18rem;
? cursor: pointer;
? transition: all 1s;
}
.list-complete-item.sortable-chosen {
? background: #4ab7bd;
}
.list-complete-item.sortable-ghost {
? background: #30b08f;
}
.undraggable {
? background-color: rgb(143, 233, 233);
}
.list-complete-enter,
.list-complete-leave-active {
? opacity: 0;
}
</style>常用事件
@start = "startChange" // 開始拖動元素觸發(fā)的事件 @end= "endChange" // 拖動元素結(jié)束觸發(fā)的事件
常用屬性配置
:options="{
?? ?group: { name: optionsName, pull: 'clone' }, // name 相同的集合子元素可以互相拖動
? ? sort: false, // 是否禁止拖動排序
? ? disabled: false, // 如果設(shè)置為真,則禁用sortable。
? ? animation: 150, ?// ms, 動畫速度運(yùn)動項(xiàng)目排序時,' 0 ' -沒有動畫。
? ? filter: ".ignore-elements", ?// 不導(dǎo)致拖拽的選擇器(字符串或函數(shù))
? ? draggable: ".item", ?// 指定元素中的哪些項(xiàng)應(yīng)該是可拖動的。
? ?? ?ghostClass: "sortable-ghost", ?// 設(shè)置拖動元素的class的占位符的類名。
? ?? ?chosenClass: "sortable-chosen", ?// 設(shè)置被選中的元素的class
? ?? ?dragClass: "sortable-drag", ?//拖動元素的class。
? }"注意事項(xiàng)
1、拖動的時間元素失去樣式
選項(xiàng)區(qū)域和目標(biāo)區(qū)域的元素樣式不一致
2、拖動失敗
選項(xiàng)區(qū)域和目標(biāo)區(qū)域的名字不一致,會導(dǎo)致拖動失敗
3、控制臺報提示( props is deprecated, add sortable options directly as vue.draggable item, or use v-bind…)
插件版本問題,:options="{}" 的寫法已經(jīng)被棄用,直接使用 v-bind 寫法,具體參照介紹
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue3中使用vuedraggable拖拽實(shí)戰(zhàn)教程
- 使用element+vuedraggable實(shí)現(xiàn)圖片上傳拖拽排序
- vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能
- vue拖拽組件vuedraggable使用說明詳解
- 使用vuedraggable實(shí)現(xiàn)從左向右拖拽功能
- vue3使用vuedraggable實(shí)現(xiàn)拖拽功能
- vuedraggable+element ui實(shí)現(xiàn)頁面控件拖拽排序效果
- vue拖拽排序插件vuedraggable使用方法詳解
- vue使用vuedraggable插件實(shí)現(xiàn)拖拽效果
相關(guān)文章
vue項(xiàng)目報錯:Missing?script:"serve"的解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目報錯:Missing?script:"serve"的解決辦法,"missing script: serve"是一個錯誤信息,意味著在執(zhí)行啟動腳本時,找不到名為"serve"的腳本,需要的朋友可以參考下2023-11-11
mpvue中使用flyjs全局?jǐn)r截的實(shí)現(xiàn)代碼
這篇文章主要介紹了mpvue中使用flyjs全局?jǐn)r截的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
在vue-cli腳手架中配置一個vue-router前端路由
這篇文章主要給大家介紹了在vue-cli腳手架中配置一個vue-router前端路由的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
Nuxt封裝@nuxtjs/axios請求后端數(shù)據(jù)方式
這篇文章主要介紹了Nuxt封裝@nuxtjs/axios請求后端數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

