作为一个效率还不错的小前端,自己的任务做完之后真的好闲啊,千盼万盼终于盼来了业务的新需求,他要我多加一个排序题,然后用户通过拖拽来排序,项目经理看我是个实习生,说有点复杂做不出来就算了,我这么闲的一个人,怎么可能做不出来,慢慢磨也能磨出来好吗。

当然一开始想向大佬们学习一手,搜了半天,实现效果不佳,我以为我真的搞不出来了,可是就在我准备自己搞的时候发现了一个大佬写好了的组件,我就去npm了一手,可是报了一堆错,于是我直接去找了人家的源码hhh,研究了一手之后,开始往我的组件里套......

因为写的是小程序嘛,因此用了小程序的movable-view,人家有可以拖拽的东西直接用何必自己乱整呢,微信开发文档里面也说了movable-view只能在movable-area里面使用,当然这里面还有一些东西要配置一下,配置好了就可以实现拖动啦,于是乎按照人家的代码依葫芦画瓢结果:

<movable-area class="drag-sort" :style="{height: currentList.length * height + 'px'}" id="drag">
<movable-view
v-for="(item, index) in currentList"
:key="index"
:x="0"
:y="item.y"
direction="vertical"
disabled
damping="40"
:animation="item.animation"
class="drag-sort-item"
style="height:50px"
@touchstart="touchstart"
@touchmove="touchmove"
@touchend="touchend"
catchtouchstart
catchtouchmove
catchtouchend
:class="{'active': active == index, 'vh-1px-t': item.index > 0}">
<view class="item">{{item.tabcontent}}</view>
</movable-view>
</movable-area>

在页面加载的时候拿到要拖拽的数组,然后结构一下加入其他需要的信息,比如下标(毕竟后端要这个顺序)

let arr = []
for (const key in this.list.tabList) {
arr.push({
...this.list.tabList[key],
index: Number(key),
y: key * this.height,
animation: true
})
}
this.currentList = arr

然后就是拖动事件balabala

/**
* 开始拖拽的位置记录
* @date 2019/09/18
*/
touchstart (e) {
// 计算y轴点击位置
var query = wx.createSelectorQuery()
query.select('#drag').boundingClientRect()
query.exec((res) => {
this.topY = res[0].top
let touchY = e.mp.touches[0].clientY - res[0].top
this.deviationY = touchY % this.height
for (const key in this.currentList) {
if ((this.currentList[key].index * this.height < touchY) && ((this.currentList[key].index + 1) * this.height > touchY)) {
this.active = key
this.index = this.currentList[key].index
break
}
}
})
},
/**
* 触摸移动
* @date 2019/09/18
*/
touchmove (e) {
if (this.active < 0) return
let touchY = (e.mp.touches[0].clientY - this.topY) - this.deviationY
this.currentList[this.active].y = touchY
this.currentList[this.active].animation = false
for (const key in this.currentList) {
// 跳过当前操作的item
if (this.currentList[key].index !== this.currentList[this.active].index) {
if (this.currentList[key].index > this.currentList[this.active].index) {
if (touchY > this.currentList[key].index * this.height - this.height / 2) {
this.currentList[this.active].index = this.currentList[key].index
this.currentList[key].index = this.currentList[key].index - 1
this.currentList[key].y = this.currentList[key].index * this.height
break
}
} else {
if (touchY < this.currentList[key].index * this.height + this.height / 2) {
this.currentList[this.active].index = this.currentList[key].index
this.currentList[key].index = this.currentList[key].index + 1
this.currentList[key].y = this.currentList[key].index * this.height
break
}
}
}
}
},
/**
* 拖拽事件结束传递参数信息给父组件
* @date 2019/09/18
*/
touchend (e) {
if ((this.index !== this.currentList[this.active].index) && (this.active > -1)) {
this.$emit('change', {
// 拖拽结束后的内容
updateList: this.currentList
})
}
this.currentList[this.active].animation = true
this.currentList[this.active].y = this.currentList[this.active].index * this.height
this.active = -1
}

一个可拖拽的组件就写好了,要什么信息再自己后期加就是了hhh

mp-vue拖拽组件的实现的更多相关文章

  1. vue拖拽组件开发

    vue拖拽组件开发 创建临时vue项目 先查看node和npm版本,怎么安装就不多多bb了 再安装vue-cli npm install vue-cli -g //全局安装 vue-cli 检测是否安 ...

  2. Vue.Draggable:基于 Sortable.js 的 Vue 拖拽组件使用中遇到的问题

    Sortable.js 介绍 https://segmentfault.com/a/1190000008209715 项目中遇到的问题: A - 我需要在项目的拖拽组件中,使用背景 1 - 想到的第一 ...

  3. Vue拖拽组件

    vue开发公众号项目,***产品需要添加一个新的功能.拖拽功能.一听简单.百度上轮子挺多,直接拉一个过来用着就行.然鹅...兴奋之余,却失望至极.东西很多,没有一个能使得.你让我失望,那我就让你绝望. ...

  4. Vue 拖拽组件 vuedraggable 和 vue-dragging

    一.描述 之前用 vue 写过一个在线的多二维码生成服务,体验地址:https://postbird.gitee.io/vue-online-qrcode/ 后面发现二维码多了之后有时候想要排序,需要 ...

  5. vue2-dragula vue拖拽组件

    https://github.com/kristianmandrup/vue2-dragula git 地址 https://github.com/kristianmandrup/vue2-dragu ...

  6. Vue拖拽组件列表实现动态页面配置

    需求描述 最近在做一个后台系统,有一个功能产品需求是页面分为左右两部分,通过右边的组件列表来动态配置左边的页面视图,并且左边由组件拼装起来的视图,可以实现上下拖拽改变顺序,也可以删除. 根据这个需求我 ...

  7. Vue 拖拽组件 vuedraggable 和 awe-dnd

    vuedraggable:https://www.npmjs.com/package/vuedraggable awe-dnd:https://www.npmjs.com/package/awe-dn ...

  8. Vue 拖拽组件 vuedraggable 、 vue-dragging 、awe-dnd

    参考链接:http://www.ptbird.cn/vue-draggable-dragging.html vue-draggable 学习和使用:https://www.jianshu.com/p/ ...

  9. Vue 可拖拽组件 Vue Smooth DnD 详解和应用演示

    本文发布自 https://www.cnblogs.com/wenruo/p/15061907.html 转载请注明出处. 简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu ...

  10. vue-slicksort拖拽组件

    vue-slicksort拖拽组件 安装 通过npm安装 $ npm install vue-slicksort --save 通过yarn安装 $ yarn add vue-slicksort 插件 ...

随机推荐

  1. 【linux】【FastDFS】FastDFS安装

    前言 FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器(storage server)和客户端(client)三个部分组成,主要解决了海量数据存 ...

  2. 从 HTTP/1 到 HTTP/2,以及即将到来的 HTTP/3

    如今的生活中已经离不开互联网,智能家居.在线支付.网上购物都需要互联网的支持.互联网切切实实地给生活带来了诸多便利.有了互联网,我们可以呆在空调房里,一边刷着微博,一边等透心凉的西瓜送到手上,安安静静 ...

  3. JQuery对于动态生成的标签绑定事件失效

    JQuery对整个html文档进行dom操作后,我们要想动态绑定事件,有两种方法 1.在进行dom操作时,在标签中写上onclick="afun()" 2.利用document的操 ...

  4. 用Python怎么SSH到网络设备

    0. 前言 自上一篇文章<用python怎么telnet到网络设备>,简单使用了telnetlib库给大家演示了下,但是,现实环境中仍不建议去使用telnet. SSH(Secure Sh ...

  5. 记一次jmeter从txt文本获取数值并给测试计划的变量赋值,jmeter永久性修改变量。

    前言: 需要永久性的改变变量. 其实这个办法并不是最好的,但是是最容易实现的.后期可做成从数据库里直接取值. 赋值BeanShell import java.io.File; import java. ...

  6. java-ztest测试报告的搭建,python-BeautifulReport

    今天用testng的时候感觉测试报告贼丑又慢,以下图片是对比.了解到ztest,搭建的时候发现网上没有教程,对java真是太不友好了,所以步骤记录下吧.有疑问的可进群:231733032 使用ztes ...

  7. mysql查询数据库中每一张表的内存大小

    SELECT TABLE_NAME,DATA_LENGTH+INDEX_LENGTH,TABLE_ROWS,concat(round((DATA_LENGTH+INDEX_LENGTH)//,), ' ...

  8. Mycat 配置文件server.xml

    server.xml 几乎保存了所有 mycat 需要的系统配置信息. 1.system 标签: 该标签内嵌套的所有 property 标签都与系统配置有关. charset 属性: 该属性用于字符集 ...

  9. ajax 请求前后处理

    1. 介绍 通过 jQuery 提供的 ajaxSetup 方法,我们可以拦截页面上所有的 Ajax 请求响应(包括 $.ajax.$.post.$.get).这样我们可以对这些 Ajax 请求响应做 ...

  10. CentOS7 Redis5.0.5环境搭建

    CentOS7 Redis5.0.5环境搭建 1基本环境配置 CentOS Linux release 7.6.1810 (Core) redis 5.0.5 1.下载解压redis.通过wget在官 ...