大家好,最近做的项目要用到拖拽排序,我现在的项目是

首先在

npm install vuedraggable -S

下载下来后,引入插件,在你的vue文件的script标签里面这样引入

import draggable from 'vuedraggable'

别忘了下面要注册组件

components: {

draggable

},

然后就可以在template标签里面使用了

{{element.text}}

下面贴一下详细用法

{{element.text}}

import draggable from 'vuedraggable'

export default{

data(){

return{

msg:"这是测试组件",

colors: [

{

text: "Aquamarine",

},

{

text: "Hotpink",

},

{

text: "Gold",

},

{

text: "Crimson",

},

{

text: "Blueviolet",

},

{

text: "Lightblue",

},

{

text: "Cornflowerblue",

},

{

text: "Skyblue",

},

{

text: "Burlywood",

}

],

startArr:[],

endArr:[],

count:0,

}

},

components: {

draggable

},

methods:{

getdata (evt) {

console.log(evt.draggedContext.element.text)

},

datadragEnd (evt) {

evt.preventDefault();

console.log('拖动前的索引 :' + evt.oldIndex)

console.log('拖动后的索引 :' + evt.newIndex)

console.log(this.colors);

}

},

mounted () {

//为了防止火狐浏览器拖拽的时候以新标签打开,此代码真实有效

document.body.ondrop = function (event) {

event.preventDefault();

event.stopPropagation();

}

}

}

.test{

border:1px solid #ccc;

}

.drag-item{

width: 200px;

height: 50px;

line-height: 50px;

margin: auto;

position: relative;

background: #ddd;

margin-top:20px;

}

.ghostClass{

opacity: 1;

}

.bottom{

width: 200px;

height: 50px;

position: relative;

background: blue;

top:2px;

left: 2px;

transition: all .5s linear;

}

下面是结果

上下是可以拖动的,只是截图的话看不出效果来,小伙伴们注意了,里面有个options选项,这个选项怎么来的呢,据我观察这个插件是基于sortable.

vuedraggable: 学习地址

sortable.

options配置如下

var sortable = new Sortable(el, {

group: "name", // or { name: "...", pull: [true, false, clone], put: [true, false, array] }

sort: true, // sorting inside list

delay: 0, // time in milliseconds to define when the sorting should start

touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event

disabled: false, // Disables the sortable if set to true.

store: null, // @see Store

animation: 150, // ms, animation speed moving items when sorting, `0` — without animation

handle: ".my-handle", // Drag handle selector within list items

filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function)

preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter`

draggable: ".item", // Specifies which items inside the element should be draggable

ghostClass: "sortable-ghost", // Class name for the drop placeholder

chosenClass: "sortable-chosen", // Class name for the chosen item

dragClass: "sortable-drag", // Class name for the dragging item

dataIdAttr: 'data-id',

forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in

fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback

fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body

fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag.

scroll: true, // or HTMLElement

scrollFn: function(offsetX, offsetY, originalEvent, touchEvt, hoverTargetEl) { ... }, // if you have custom scrollbar scrollFn may be used for autoscrolling

scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling.

scrollSpeed: 10, // px

setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) {

dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent

},

// Element is chosen

onChoose: function (/**Event*/evt) {

evt.oldIndex; // element index within parent

},

// Element dragging started

onStart: function (/**Event*/evt) {

evt.oldIndex; // element index within parent

},

// Element dragging ended

onEnd: function (/**Event*/evt) {

var itemEl = evt.item; // dragged HTMLElement

evt.to; // target list

evt.from; // previous list

evt.oldIndex; // element's old index within old parent

evt.newIndex; // element's new index within new parent

},

// Element is dropped into the list from another list

onAdd: function (/**Event*/evt) {

// same properties as onEnd

},

// Changed sorting within list

onUpdate: function (/**Event*/evt) {

// same properties as onEnd

},

// Called by any change to the list (add / update / remove)

onSort: function (/**Event*/evt) {

// same properties as onEnd

},

// Element is removed from the list into another list

onRemove: function (/**Event*/evt) {

// same properties as onEnd

},

// Attempt to drag a filtered element

onFilter: function (/**Event*/evt) {

var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event.

},

// Event when you move an item in the list or between lists

onMove: function (/**Event*/evt, /**Event*/originalEvent) {

// Example: http://jsbin.com/tuyafe/1/edit?js,output

evt.dragged; // dragged HTMLElement

evt.draggedRect; // TextRectangle {left, top, right и bottom}

evt.related; // HTMLElement on which have guided

evt.relatedRect; // TextRectangle

originalEvent.clientY; // mouse position

// return false; — for cancel

},

// Called when creating a clone of element

onClone: function (/**Event*/evt) {

var origEl = evt.item;

var cloneEl = evt.clone;

}

});

好了,今天的介绍就到这里啦,快去试试吧。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持前端开发者。

html5+vue无法拖拽,Vue拖拽排序插件Vuedraggable使用方法详解_心善_前端开发者相关推荐

  1. vue 子级拿值_vue 父组件通过$refs获取子组件的值和方法详解

    前言 在vue项目中组件之间的通讯是很常见的问题,同时也是很重要的问题,我们大致可以将其分为三种情况: 父传子:在父组件中绑定值,在子组件中用props接收 子传父:在父组件中监听一个事件,在子组件中 ...

  2. vue ajax highcharts,在vue项目中引入highcharts图表的方法(详解)

    npm进行highchars的导入,导入完成后就可以进行highchars的可视化组件开发了 npm install highcharts --save 1.components目录下新建一个char ...

  3. keep alive PHP,vue中keep-alive使用方法详解

    这次给大家带来vue中keep-alive使用方法详解,vue中keep-alive使用的注意事项有哪些,下面就是实战案例,一起来看一下. 1.keep-alive的作用以及好处 在做电商有关的项目中 ...

  4. vue设置html自动跳转路由器,vue2.0项目实现路由跳转的方法详解

    一.安装 1.安装路由vue-router: npm install vue-router vue项目的依赖文件node_modules存在vue-router依赖,说明安装成功 2.vue项目引入v ...

  5. vue 生命周期的11中方法详解

    vue 生命周期的十一中方法详解 vue 生命周期的定义 vue实例 从创建到销毁,也就是说从创建 -> 初始化 数据-> 编译模版 -> 挂载Dom -> 渲染 更新 -&g ...

  6. vue ajax拦截器,Vue-resource拦截器判断token失效跳转详解

    本文主要为大家带来一篇Vue-resource拦截器判断token失效跳转的实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧,希望能帮助到大家. 在拦截器中设置全局的 ...

  7. CSS3 渐变新特性和HTML5 Canvas画布背景渐变实现方法详解

    CSS3 渐变新特性和HTML5 Canvas画布背景渐变实现方法详解 大家好,又见面了,感觉我上一篇博客对部分同学都挺有帮助的,于是我决定继续写下去,会继续的解析知识点让大家更容易理解,希望能给大家 ...

  8. sortable vue 排序_Vue使用Sortable步骤详解

    这次给大家带来Vue使用Sortable步骤详解,Vue使用Sortable的注意事项有哪些,下面就是实战案例,一起来看一下. 之前开发一个后台管理系统,里面用到了Vue和Element-UI这个组件 ...

  9. vuepdf转换html,Vue网页html转换PDF(最低兼容ie10)的思路详解

    Vue网页html转换PDF(最低兼容ie10)的思路详解 发布时间:2020-10-16 13:05:09 来源:脚本之家 阅读:95 作者:冷藏封 HTML转PDF: 1.页面底层实现--Vue: ...

最新文章

  1. 基于Transformers入门自然语言处理!
  2. jquery选中checkbox
  3. 修改C:\WINDOWS\system32\drivers\etc\hosts 文件有什么作用
  4. LDR指令和LDR伪指令区别
  5. HDU-1241 Oil Deposits (DFS)
  6. npm ERR! gifsicle@5.2.0 postinstall: `node lib/install.js`
  7. YBTOJ:公共子串(KMP)
  8. Spring Cloud 升级最新 Finchley 版本,踩了所有的坑
  9. LazyInitializationException的四种解决方案–第2部分
  10. 关于IP转换器无法正常启动的问题
  11. 新手CrossApp 之CAProgress小结
  12. Securing Android: A Survey, Taxonomy, and Challenges --论文笔记
  13. html怎样设置图片的圆角矩形,怎么把矩形变成圆角 ps怎么在原来的矩形中改成圆角...
  14. ipad显示已停用,连接itunes
  15. 2021年计算机软考时间公布啦
  16. matlab print用法,使用Matlab:错误使用 mprint (line 231) Wrong # rnames in mprint,怎么解决...
  17. DBeaver中的常用快捷键和自定义快捷键方式
  18. PCB设计入门—学习记录
  19. Nginx代理——正向、反向代理,动静分离和负载均衡
  20. MySQL : Access denied for user ''@'localhost' to database 'mysql'问题看点。

热门文章

  1. Win11系统提示找不到msvcr120.dll文件解决办法
  2. js开始摄像头并获取数据
  3. java象棋游戏用户特点_java-象棋游戏设计心得
  4. DeepMind 提出“神经算术逻辑单元”,功能强大引发热议
  5. 约瑟夫斯问题,十步杀一人(环形队列)
  6. python矩阵运算程序_用于矩阵运算的Python程序
  7. 腾讯微博10年数据对比,看看过去的辉煌,曾经的全球第一微博
  8. 【SPSS】两配对样本T检验分析详细操作教程(附案例实战)
  9. 计算机革命的意义,计算机革命的哲学
  10. Dart学习日记-基础类型