vue2.0使用Sortable.js實(shí)現(xiàn)的拖拽功能示例
簡介
在使用vue1.x之前的版本的時候,頁面中的拖拽功能,我在項(xiàng)目中是直接用的jquery ui中的sortable.js,只是在拖拽完成后,在update的回調(diào)函數(shù)中又重新排序了存放數(shù)據(jù)的數(shù)組。但是當(dāng)把vue升級到2.0以上后發(fā)現(xiàn)拖拽功能失效了,于是使用了下面代碼。
該案例主要是在用于vuejs2.0中實(shí)現(xiàn)的拖拽功能,用到的的js有Sortable.js,vuedraggable.js,當(dāng)然還有vue.min.js,提供的案例使用的require.js加載。
實(shí)現(xiàn)效果
實(shí)現(xiàn)后的效果如圖所示:
html主要代碼
<draggable :list="list2" :move="getdata" @update="datadragEnd" :options="{animation: 300,handle:'.dargDiv'}"> <transition-group name="list-complete" > <div v-for="element in list2" :key="element.it.name" class="list-complete-item"> <div class="styleclass dargDiv">{{element.id}}</div> <div class="styleclass">{{element.it.name}}</div> </div> </transition-group> </draggable>
css代碼
body{ font-family:'微軟雅黑' } [v-cloak]{ display:none; } #example{ width:1000px; margin:0 auto; } .list-complete-item { transition: all 1s; height:50px; line-height: 50px; background: #000; color:#fff; text-align: center; font-size:24px; margin-top:10px; } .styleclass{ width:100px; float:left; } .list-complete-enter, .list-complete-leave-active { opacity: 0; height: 0px; margin-top: 0px; padding: 0px; border: solid 0px; } .list-complete-sortable-chosen,.list-complete-sortable-ghost{ opacity: 0; height: 0px; margin-top: 0px; padding: 0px; border: solid 0px; } .dargDiv{ cursor:move; background:red; } .wrods{ margin-top:50px; } p{ line-height:24px; text-align:center; }
js代碼
require.config({ urlArgs: "ver=1.0_0", paths:{ "vue":'vue.min2', "sortablejs":'Sortable', "vuedraggable":'vuedraggable' }, shim:{ 'vue':{ exports:'vue' } } }), require(['vue','vuedraggable'],function(Vue,draggable){ Vue.component('draggable', draggable); new Vue({ el: '#example', data: { list2:[ {id:"id1",it:{name:'bbbb'}}, {id:"id2",it:{name:'2222'}}, {id:"id3",it:{name:'3333'}}, {id:"id4",it:{name:'4444'}} ] }, methods:{ getdata: function(evt){ console.log(evt.draggedContext.element.id); }, datadragEnd:function(evt){ console.log('拖動前的索引:'+evt.oldIndex); console.log('拖動后的索引:'+evt.newIndex); } } }) })
里面的可配置的很多細(xì)節(jié)請參考參考地址,這里不做詳細(xì)介紹。
可下載案例地址:Vue.Draggable-case_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+el-table封裝示例詳解(編輯、刪除、查看詳情按鈕一起封裝)
在Vue3中,利用Element?Plus?UI庫封裝表格組件,實(shí)現(xiàn)編輯、刪除和查看詳情的功能,通過定義tableData和tableDataHeader來管理表格數(shù)據(jù)和表頭,其中tableData通常從后端獲取,而tableHeader可根據(jù)具體需求自定義,感興趣的朋友跟隨小編一起看看吧2024-09-09vue同一個瀏覽器登錄不同賬號數(shù)據(jù)覆蓋問題解決方案
同一個瀏覽器登錄不同賬號session一致,這就導(dǎo)致后面登錄的用戶數(shù)據(jù)會把前面登錄的用戶數(shù)據(jù)覆蓋掉,這個問題很常見,當(dāng)前我這邊解決的就是同一個瀏覽器不同窗口只能登錄一個用戶,對vue同一個瀏覽器登錄不同賬號數(shù)據(jù)覆蓋問題解決方法感興趣的朋友一起看看吧2024-01-01Vue 項(xiàng)目代理設(shè)置的優(yōu)化
這篇文章主要介紹了Vue 項(xiàng)目代理設(shè)置的優(yōu)化功能,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04VUE中的export default和export使用方法解析
export default和export都能導(dǎo)出一個模塊里面的常量,函數(shù),文件,模塊等,在其它文件或模塊中通過import來導(dǎo)入常量,函數(shù),文件或模塊。但是,在一個文件或模塊中export,import可以有多個,export default卻只能有一個。2022-12-12