vueDraggable-基础01

464 阅读3分钟

vueDraggable

Vue.Draggable是一款基于Sortable.js实现的vue拖拽插件。

支持移动设备、拖拽和选择文本、智能滚动。

可以在不同列表间拖拽、不依赖jQuery为基础、vue2过渡动画兼容、支持撤销操作,总之是一款非常优秀的vue拖拽组件。本篇将介绍如何搭建环境及简单的例子,使用起来特别简单对被拖拽元素也没有CSS样式的特殊要求。

文档地址

官方网站 sortable文档 vuedraggable文档

安装方式

---项目vscode安装---
yarn add vuedraggable
npm i -S vuedraggable

---直接引用----
<script src="https://www.itxst.com/package/vue/vue.min.js"></script>
<script src="https://www.itxst.com/package/sortable/Sortable.min.js"></script>
<script src="https://www.itxst.com/package/vuedraggable/vuedraggable.umd.min.js"></script>

属性说明

属性名称说明
group:group= "name",相同的组之间可以相互拖拽 或者 { name: "...", pull: [true, false, 'clone', array , function], put: [true, false, array , function] }
sort:sort= "true",是否开启内部排序,如果设置为false,它所在组无法排序,在其他组可以拖动排序
delay:delay= "0", 鼠标按下后多久可以拖拽
touchStartThreshold鼠标移动多少px才能拖动元素
disabled:disabled= "true",是否启用拖拽组件
animation拖动时的动画效果,还是很酷的,数字类型。如设置animation=1000表示1秒过渡动画效果
handle:handle=".mover" 只有当鼠标移动到css为mover类的元素上才能拖动
filter:filter=".unmover" 设置了unmover样式的元素不允许拖动
draggable:draggable=".item" 那些元素是可以被拖动的
ghostClass:ghostClass="ghostClass" 设置拖动元素的占位符类名,你的自定义样式可能需要加!important才能生效,并把forceFallback属性设置成true
chosenClass:ghostClass="hostClass" 被选中目标的样式,你的自定义样式可能需要加!important才能生效,并把forceFallback属性设置成true
dragClass:dragClass="dragClass"拖动元素的样式,你的自定义样式可能需要加!important才能生效,并把forceFallback属性设置成true
dataIdAttrdataIdAttr: 'data-id'
forceFallback默认false,忽略HTML5的拖拽行为,因为h5里有个属性也是可以拖动,你要自定义ghostClass chosenClass dragClass样式时,建议forceFallback设置为true
fallbackClass默认false,克隆的DOM元素的类名
allbackOnBody默认false,克隆的元素添加到文档的body中
fallbackTolerance拖拽之前应该移动的px
scroll默认true,有滚动区域是否允许拖拽
scrollFn滚动回调函数
scrollSensitivity距离滚动区域多远时,滚动滚动条
scrollSpeed滚动速度

事件

参数说明回调参数
start开始拖动时的回调函数function({to,from,item,clone,oldIndex,newIndex})
add添加单元时的回调函数function({to,from,item,clone,oldIndex,newIndex})
remove单元被移动到另一个列表时的回调函数function({to,from,item,clone,oldIndex,newIndex})
update排序发生变化时的回调函数function({to,from,item,clone,oldIndex,newIndex})
end拖动结束时的回调函数function({to,from,item,clone,oldIndex,newIndex})
choose选择单元时的回调函数function({to,from,item,clone,oldIndex,newIndex})
sort排序发生变化时的回调函数function({to,from,item,clone,oldIndex,newIndex})
filter尝试选择一个被filter过滤的单元的回调函数function({to,from,item,clone,oldIndex,newIndex})
cloneclone时的回调函数function({to,from,item,clone,oldIndex,newIndex})

基础演示

代码详情点击,可以查看到详细的代码。

系列相关推荐:

vueDraggable-基础01

vueDraggable-基础02

vueDraggable-属性01

vueDraggable-属性02

vueDraggable-属性(最后)