您好,登錄后才能下訂單哦!
這篇文章主要講解了“VUE怎么使用draggable實(shí)現(xiàn)組件拖拽”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“VUE怎么使用draggable實(shí)現(xiàn)組件拖拽”吧!
實(shí)現(xiàn)步驟
1、導(dǎo)入draggable依賴
npm i -S vuedraggable
2、引入draggable
import draggable from "vuedraggable"
3、注冊(cè)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, //決定是否可拖動(dòng),為false則組件不可拖動(dòng)
isDragging: false,
};
},
定義移動(dòng)方法
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">組件拖拽實(shí)例</h4>
</div>
<div class="panel-body">
<div class="checkbox">
<label
><input type="checkbox" v-model="editable" />設(shè)置組件可拖拽</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, //決定是否可拖動(dòng),為false則組件不可拖動(dòng)
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實(shí)現(xiàn)組件拖拽”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)VUE怎么使用draggable實(shí)現(xiàn)組件拖拽這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。