vue2.0使用Sortable.js實(shí)現(xiàn)的拖拽功能示例
簡(jiǎn)介
在使用vue1.x之前的版本的時(shí)候,頁面中的拖拽功能,我在項(xiàng)目中是直接用的jquery ui中的sortable.js,只是在拖拽完成后,在update的回調(diào)函數(shù)中又重新排序了存放數(shù)據(jù)的數(shù)組。但是當(dāng)把vue升級(jí)到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('拖動(dòng)前的索引:'+evt.oldIndex);
console.log('拖動(dòng)后的索引:'+evt.newIndex);
}
}
})
})
里面的可配置的很多細(xì)節(jié)請(qǐng)參考參考地址,這里不做詳細(xì)介紹。
可下載案例地址:Vue.Draggable-case_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Element Cascader 級(jí)聯(lián)選擇器的使用示例
這篇文章主要介紹了Element Cascader 級(jí)聯(lián)選擇器的使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue封裝實(shí)現(xiàn)自動(dòng)循環(huán)滾動(dòng)的列表
在做數(shù)據(jù)大屏開發(fā)的過程中,經(jīng)常出現(xiàn)需要對(duì)列表進(jìn)行自動(dòng)滾動(dòng)的需求,所以本文就來為大家介紹一下如何利用vue封裝一個(gè)自動(dòng)循環(huán)滾動(dòng)的列表吧2023-09-09
Vue.js中使用components組件的實(shí)例講解
這篇文章主要介紹了Vue.js中使用components組件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue+epubjs實(shí)現(xiàn)電子書閱讀器的基本功能
這篇文章主要為大家詳細(xì)介紹了如何使用vue+epubjs實(shí)現(xiàn)電子書閱讀器的基本功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
vue+elementui 實(shí)現(xiàn)上傳文件/導(dǎo)入文件的功能示例詳解
文章介紹了如何使用Vue和Element UI實(shí)現(xiàn)上傳文件和導(dǎo)入文件的功能,主要內(nèi)容包括:上傳組件的使用、data中的數(shù)據(jù)存儲(chǔ)、methods中的方法(選擇文件、點(diǎn)擊確定上傳文件、刪除文件),文章還提供了相關(guān)鏈接供進(jìn)一步學(xué)習(xí),感興趣的朋友一起看看吧2025-03-03
Vue3+Ts實(shí)現(xiàn)父子組件間傳值的兩種方式
這篇文章主要給大家介紹了基于Vue3+Ts實(shí)現(xiàn)父子組件間傳值的兩種方式,使用v-model+emit傳值和使用v-bind+emit傳值兩種方式,文章通過代碼示例介紹的非常詳細(xì),感興趣的同學(xué)可以參考閱讀2023-09-09
vue.js指令v-model實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了vue.js指令v-model實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
web前端vue之vuex單獨(dú)一文件使用方式實(shí)例詳解
Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。這篇文章主要介紹了web前端vue:vuex單獨(dú)一文件使用方式,需要的朋友可以參考下2018-01-01

