您好,登錄后才能下訂單哦!
這篇文章主要講解了“VUE怎么使用draggable實現組件拖拽”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“VUE怎么使用draggable實現組件拖拽”吧!
實現步驟
1、導入draggable依賴
npm i -S vuedraggable
2、引入draggable
import draggable from "vuedraggable"
3、注冊draggable
components: {
draggable
},
4、使用draggable
html頁面
<div class="col-md-3">
<draggable
class="list-group"
tag="ul"
:list="this.list"
v-bind="dragOptions"
:move="onMove"
@start="isDragging = true"
@end="isDragging = false"
>
<div
class="list-group-item"
v-for="(item, index) in this.list"
:key="item.value"
>
<span class="badge">{{ index }}</span>
<span class="lefttitle">
{{ item.name }}
</span>
</div>
</draggable>
</div>
定義屬性和list
data() {
return {
list: [
{
name: "-姓名",
value: "name",
},
{
name: "-性別",
value: "sex",
},
{
name: "-年齡",
value: "age",
},
{
name: "-地址",
value: "address",
},
{
name: "-郵箱",
value: "mailbox",
},
],
editable: true, //決定是否可拖動,為false則組件不可拖動
isDragging: false,
};
},
定義移動方法
methods: {
onMove({ relatedContext, draggedContext }) {
const relatedElement = relatedContext.element;
const draggedElement = draggedContext.element;
return (
(!relatedElement || !relatedElement.fixed) && !draggedElement.fixed
);
},
},
5、完整VUE代碼
<template>
<div class="fluid container">
<div class="form-group form-group-lg panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">組件拖拽實例</h4>
</div>
<div class="panel-body">
<div class="checkbox">
<label
><input type="checkbox" v-model="editable" />設置組件可拖拽</label
>
</div>
</div>
</div>
<div class="col-md-3">
<draggable
class="list-group"
tag="ul"
:list="this.list"
v-bind="dragOptions"
:move="onMove"
@start="isDragging = true"
@end="isDragging = false"
>
<div
class="list-group-item"
v-for="(item, index) in this.list"
:key="item.value"
>
<span class="badge">{{ index }}</span>
<span class="lefttitle">
{{ item.name }}
</span>
</div>
</draggable>
</div>
<div class="list-group col-md-3">
<pre>{{ listString }}</pre>
</div>
</div>
</template>
<script>
import draggable from "vuedraggable";
export default {
components: {
draggable,
},
data() {
return {
list: [
{
name: "-姓名",
value: "name",
},
{
name: "-性別",
value: "sex",
},
{
name: "-年齡",
value: "age",
},
{
name: "-地址",
value: "address",
},
{
name: "-郵箱",
value: "mailbox",
},
],
list2: [],
editable: true, //決定是否可拖動,為false則組件不可拖動
isDragging: false,
delayedDragging: false,
};
},
methods: {
onMove({ relatedContext, draggedContext }) {
const relatedElement = relatedContext.element;
const draggedElement = draggedContext.element;
return (
(!relatedElement || !relatedElement.fixed) && !draggedElement.fixed
);
},
},
computed: {
dragOptions() {
return {
animation: 0,
group: "description",
disabled: !this.editable,
ghostClass: "ghost",
};
},
listString() {
return JSON.stringify(this.list, null, 2);
},
},
watch: {
isDragging(newValue) {
if (newValue) {
this.delayedDragging = true;
return;
}
this.$nextTick(() => {
this.delayedDragging = false;
});
},
},
};
</script>
<style>
.flip-list-move {
transition: transform 0.5s;
}
.no-move {
transition: transform 0s;
}
.ghost {
opacity: 0.5;
background: #c8ebfb;
}
.list-group {
min-height: 20px;
}
.list-group-item {
cursor: move;
}
.list-group-item i {
cursor: pointer;
}
</style>
感謝各位的閱讀,以上就是“VUE怎么使用draggable實現組件拖拽”的內容了,經過本文的學習后,相信大家對VUE怎么使用draggable實現組件拖拽這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。