vue拖拽添加的簡(jiǎn)單實(shí)現(xiàn)
本文主要介紹了vue拖拽添加的簡(jiǎn)單實(shí)現(xiàn),具體如下:
效果圖
并沒(méi)有判斷是否重復(fù),沒(méi)有刪除舊數(shù)據(jù)

數(shù)據(jù)體
<MyShuttle :dataOrigin='[
{
Name:"數(shù)據(jù)001",
Id:"數(shù)001",
},
{
Name:"數(shù)據(jù)002",
Id:"數(shù)001",
},
{
Name:"數(shù)據(jù)003",
Id:"數(shù)001",
}]'
:space='[{
Name:"右001",
Id:"右001",
}]' />
代碼
draggable開(kāi)啟可拖動(dòng)
@dragenter.prevent @dragover.prevent // 阻止瀏覽器默認(rèn)行為,不然會(huì)顯示一個(gè)叉叉,不好看
阻止默認(rèn)行為
@dragleave.stop=“dragleave($event, ‘main')”
進(jìn)入離開(kāi)當(dāng)前元素都會(huì)觸發(fā)
@dragend.stop=“dragEnd($event, item)”
放下拖拽內(nèi)容觸發(fā)
拖拽事件和屬性
標(biāo)記?這個(gè)很重要!!! 這個(gè)決定了拖拽事件的行為。當(dāng)點(diǎn)擊開(kāi)始拖拽之后,鼠標(biāo)點(diǎn)擊所在的位置就是標(biāo)記。
dragstart:當(dāng)單擊下鼠標(biāo),并移動(dòng)之后執(zhí)行。
drag:在dragstart執(zhí)行之后,鼠標(biāo)在移動(dòng)時(shí)連續(xù)觸發(fā)。
dragend:當(dāng)拖拽行為結(jié)束,也就是松開(kāi)鼠標(biāo)的時(shí)候觸發(fā)。
dragenter:當(dāng)正在拖拽的元素的標(biāo)記進(jìn)入某個(gè)Dom元素時(shí)觸發(fā),自身首先會(huì)觸發(fā)。被進(jìn)入的Dom元素會(huì)觸發(fā)這個(gè)事件。
dragover:當(dāng)拖拽的元素的標(biāo)記在進(jìn)入的Dom元素上移動(dòng)時(shí)觸發(fā),在自身移動(dòng)時(shí)也會(huì)觸發(fā)。
dragleave:當(dāng)拖拽的元素在離開(kāi)進(jìn)入的Dom時(shí)觸發(fā)。
H5拖拽屬性 draggable
draggable:當(dāng)需要某個(gè)元素可以拖拽時(shí),需設(shè)置為true,默認(rèn)為false。選中的文本、圖片、鏈接默認(rèn)可以拖拽。
DataTransfer對(duì)象:該屬性用于保存拖放的數(shù)據(jù)和交互信息,該組件沒(méi)有使用到,暫忽略。
當(dāng)鼠標(biāo)移動(dòng)到目標(biāo)div盒子才會(huì)追加,簡(jiǎn)單的才最能說(shuō)明問(wèn)題
<template>
<div class="MyShuttle">
<div class="MyShuttleLeft">
<div class="title">數(shù)據(jù)源</div>
<div class="dataBox" @dragleave.stop="dragleave($event, 'main')">
<div v-for="(item, i) in dataOrigin" :key="i" class="dataList" draggable @dragenter.prevent
@dragover.prevent @dragstart.stop="dragstart($event, item)"
@dragend.stop="dragEnd($event, item)">
{{ item.Name}}
</div>
</div>
</div>
<div class="MyShuttleCenter"></div>
<div class="MyShuttleRight">
<div class="title">數(shù)據(jù)源</div>
<div ref="MyShuttleRight" class="dataBox">
<div v-for="(item, i) in spaceList" :key="i" class="dataList" draggable @dragenter.prevent
@dragover.prevent>
{{ item.Name}}
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: '',
components: {},
props: {
dataOrigin: {
type: Array
},
space: {
type: Array
}
},
data() {
return {
spaceList: this.space,
isDragStatus: false
}
},
computed: {},
watch: {},
created() { },
mounted() { },
methods: {
dragleave(e, item) {
// console.log(e, item)
if (item === 'main') {
this.isDragStatus = true
} else {
this.isDragStatus = false
}
// console.log(this.isDragStatus)
},
dragstart(e, item) {
// console.log(e, item)
},
dragEnd(e, item) {
const top = this.$refs.MyShuttleRight.getBoundingClientRect().top
const right = this.$refs.MyShuttleRight.getBoundingClientRect().right
const bottom = this.$refs.MyShuttleRight.getBoundingClientRect().bottom
const left = this.$refs.MyShuttleRight.getBoundingClientRect().left
console.log(top, right, bottom, left)
console.log(e.clientX, e.clientY, item)
if (this.isDragStatus && e.clientY > top && e.clientY < bottom && e.clientX > left && e.clientX < right) {
this.spaceList.push(item)
console.log(this.spaceList.indexOf(item))
}
}
}
}
</script>
<style scoped lang="scss">
.MyShuttle {
width: 100%;
height: 308px;
display: flex;
justify-content: space-between;
// 左右盒子公共樣式
.MyShuttleLeft,
.MyShuttleRight {
border: 1px solid #dddddd;
border-collapse: collapse;
.title {
box-sizing: border-box;
width: 100%;
height: 40px;
background: #f5f5f5;
border-radius: 4px 4px 0px 0px;
border-bottom: 1px solid #dddddd;
padding: 10px 16px;
font-size: 14px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #333333;
line-height: 20px;
}
.dataBox {
width: 100%;
height: 228px;
overflow: auto;
padding: 6px 0;
.dataList {
width: 96%;
height: 40px;
box-sizing: border-box;
font-size: 14px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #666;
line-height: 20px;
margin: 0 2% 10px;
padding: 10px 16px;
border: 1px solid #ddd;
border-radius: 4px;
user-select: none;
cursor: pointer;
&:hover {
color: #01bc77;
background: rgba(1, 188, 119, 0.1);
}
}
}
}
.MyShuttleLeft {
width: 362px;
height: 100%;
background: #ffffff;
border-radius: 4px;
}
.MyShuttleCenter {
width: 64px;
height: 100%;
}
.MyShuttleRight {
width: 362px;
height: 100%;
background: #ffffff;
border-radius: 4px;
}
}
</style>
到此這篇關(guān)于vue拖拽添加的簡(jiǎn)單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue拖拽添加內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!?
相關(guān)文章
Vue.js組件使用props傳遞數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了Vue.js組件使用props傳遞數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
淺談vue權(quán)限管理實(shí)現(xiàn)及流程
這篇文章主要介紹了淺談vue權(quán)限管理實(shí)現(xiàn)及流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
JSON數(shù)組和JSON對(duì)象在vue中的獲取方法
這兩天在學(xué)習(xí)vue,主要是為了實(shí)現(xiàn)前后端的分離,因此數(shù)據(jù)的傳輸是必不可少的一個(gè)環(huán)節(jié),這篇文章主要介紹了JSON數(shù)組和JSON對(duì)象在vue中的獲取方法,需要的朋友可以參考下2022-09-09
Element InfiniteScroll無(wú)限滾動(dòng)的具體使用方法
這篇文章主要介紹了Element InfiniteScroll無(wú)限滾動(dòng)的具體使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue前端優(yōu)雅展示后端十萬(wàn)條數(shù)據(jù)面試點(diǎn)剖析
這篇文章主要為大家介紹了vue前端優(yōu)雅展示后端十萬(wàn)條數(shù)據(jù)的考點(diǎn)剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
詳解Vue CLI 3.0腳手架如何mock數(shù)據(jù)
這篇文章主要介紹了詳解Vue CLI 3.0腳手架如何mock數(shù)據(jù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

