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文件中監(jiān)聽input輸入事件(oninput)
本篇文章主要介紹了詳解.vue文件中監(jiān)聽input輸入事件(oninput),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09vue3給動(dòng)態(tài)渲染的組件添加ref的解決方案
ref和reactive一樣,也是用來實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)的方法,下面這篇文章主要給大家介紹了關(guān)于vue3給動(dòng)態(tài)渲染的組件添加ref的解決方案,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11vuex實(shí)現(xiàn)登錄狀態(tài)的存儲(chǔ),未登錄狀態(tài)不允許瀏覽的方法
下面小編就為大家分享一篇vuex實(shí)現(xiàn)登錄狀態(tài)的存儲(chǔ),未登錄狀態(tài)不允許瀏覽的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03Vue解決element-ui消息提示$message重疊問題
這篇文章主要為大家介紹了Vue解決element-ui消息提示$message重疊問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08vue3實(shí)現(xiàn)動(dòng)態(tài)路由及菜單
這篇文章主要介紹了vue3實(shí)現(xiàn)動(dòng)態(tài)路由及菜單,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03