vue2.0使用Sortable.js实现的拖拽功能示例

简介

在使用vue1.x之前的版本的时候,页面中的拖拽功能,我在项目中是直接用的jquery ui中的sortable.js,只是在拖拽完成后,在update的回调函数中又重新排序了存放数据的数组。但是当把vue升级到2.0以上后发现拖拽功能失效了,于是使用了下面代码

该案例主要是在用于vuejs2.0中实现的拖拽功能,用到的的js有Sortable.js,vuedraggable.js,当然还有vue.min.js,提供的案例使用的require.js加载。

实现效果

实现后的效果如图所示:

html主要代码

rush:xhtml;">

css代码

rush: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代码

rush: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);
  }

}

})

})

里面的可配置的很多细节请参考参考地址,这里不做详细介绍。

可下载案例地址:vue.draggable-case_jb51.rar">vue.draggable-case_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

这篇文章我们将通过debug源码的方式来带你搞清楚defineAsync...
欧阳老老实实的更新自己的高质量vue源码文章,还被某2.6k st...
前言 在Vue3.5版本中响应式 Props 解构终于正式转正了,这个...
组合式 (Composition) API 的一大特点是“非常灵活”,但也因...
相信你最近应该看到了不少介绍Vue Vine的文章,这篇文章我们...
前言 在欧阳的上一篇 这应该是全网最详细的Vue3.5版本解读文...