vue實(shí)現(xiàn)div拖拽互換位置
更新時(shí)間:2020年07月29日 09:02:45 作者:無花即無果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)div拖拽互換位置的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了vue實(shí)現(xiàn)div拖拽互換位置的具體代碼,供大家參考,具體內(nèi)容如下
template模板
<transition-group tag="div" class="container">
<div class="item" v-for="(item,index) in items" :key="item.key" :style="{background:item.color,width:'80px',height:'80px'}"
draggable="true"
@dragstart="handleDragStart($event, item)"
@dragover.prevent="handleDragOver($event, item)"
@dragenter="handleDragEnter($event, item)"
@dragend="handleDragEnd($event, item)" >
</div>
</transition-group>
script:
<script>
export default {
name: 'Toolbar',
data () {
return {
items: [
{ key: 1, color: '#ffebcc'},
{ key: 2, color: '#ffb86c'},
{ key: 3, color: '#f01b2d'}
],
dragging: null
}
},
methods:{
handleDragStart(e,item){
this.dragging = item;
},
handleDragEnd(e,item){
this.dragging = null
},
//首先把div變成可以放置的元素,即重寫dragenter/dragover
handleDragOver(e) {
e.dataTransfer.dropEffect = 'move'// e.dataTransfer.dropEffect="move";//在dragenter中針對(duì)放置目標(biāo)來設(shè)置!
},
handleDragEnter(e,item){
e.dataTransfer.effectAllowed = "move"http://為需要移動(dòng)的元素設(shè)置dragstart事件
if(item === this.dragging){
return
}
const newItems = [...this.items]
console.log(newItems)
const src = newItems.indexOf(this.dragging)
const dst = newItems.indexOf(item)
newItems.splice(dst, 0, ...newItems.splice(src, 1))
this.items = newItems
}
}
}
</script>
<style scoped>
.container{
width: 80px;
height: 300px;
position: absolute;
left: 0;
display:flex;
flex-direction: column;
padding: 0;
}
.item {
margin-top: 10px;
transition: all linear .3s
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Vue項(xiàng)目在其他電腦npm run dev運(yùn)行報(bào)錯(cuò)的解決方法
這篇文章主要介紹了詳解Vue項(xiàng)目在其他電腦npm run dev運(yùn)行報(bào)錯(cuò)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
django使用channels2.x實(shí)現(xiàn)實(shí)時(shí)通訊
這篇文章主要介紹了django使用channels2.x實(shí)現(xiàn)實(shí)時(shí)通訊,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
Vue開發(fā)手冊(cè)Function-based?API?RFC
這篇文章主要為大家介紹了Vue開發(fā)手冊(cè)Function-based?API?RFC使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

