mp-vue拖拽组件的实现
作为一个效率还不错的小前端,自己的任务做完之后真的好闲啊,千盼万盼终于盼来了业务的新需求,他要我多加一个排序题,然后用户通过拖拽来排序,项目经理看我是个实习生,说有点复杂做不出来就算了,我这么闲的一个人,怎么可能做不出来,慢慢磨也能磨出来好吗。
当然一开始想向大佬们学习一手,搜了半天,实现效果不佳,我以为我真的搞不出来了,可是就在我准备自己搞的时候发现了一个大佬写好了的组件,我就去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拖拽组件的实现的更多相关文章
- vue拖拽组件开发
vue拖拽组件开发 创建临时vue项目 先查看node和npm版本,怎么安装就不多多bb了 再安装vue-cli npm install vue-cli -g //全局安装 vue-cli 检测是否安 ...
- Vue.Draggable:基于 Sortable.js 的 Vue 拖拽组件使用中遇到的问题
Sortable.js 介绍 https://segmentfault.com/a/1190000008209715 项目中遇到的问题: A - 我需要在项目的拖拽组件中,使用背景 1 - 想到的第一 ...
- Vue拖拽组件
vue开发公众号项目,***产品需要添加一个新的功能.拖拽功能.一听简单.百度上轮子挺多,直接拉一个过来用着就行.然鹅...兴奋之余,却失望至极.东西很多,没有一个能使得.你让我失望,那我就让你绝望. ...
- Vue 拖拽组件 vuedraggable 和 vue-dragging
一.描述 之前用 vue 写过一个在线的多二维码生成服务,体验地址:https://postbird.gitee.io/vue-online-qrcode/ 后面发现二维码多了之后有时候想要排序,需要 ...
- vue2-dragula vue拖拽组件
https://github.com/kristianmandrup/vue2-dragula git 地址 https://github.com/kristianmandrup/vue2-dragu ...
- Vue拖拽组件列表实现动态页面配置
需求描述 最近在做一个后台系统,有一个功能产品需求是页面分为左右两部分,通过右边的组件列表来动态配置左边的页面视图,并且左边由组件拼装起来的视图,可以实现上下拖拽改变顺序,也可以删除. 根据这个需求我 ...
- Vue 拖拽组件 vuedraggable 和 awe-dnd
vuedraggable:https://www.npmjs.com/package/vuedraggable awe-dnd:https://www.npmjs.com/package/awe-dn ...
- Vue 拖拽组件 vuedraggable 、 vue-dragging 、awe-dnd
参考链接:http://www.ptbird.cn/vue-draggable-dragging.html vue-draggable 学习和使用:https://www.jianshu.com/p/ ...
- Vue 可拖拽组件 Vue Smooth DnD 详解和应用演示
本文发布自 https://www.cnblogs.com/wenruo/p/15061907.html 转载请注明出处. 简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu ...
- vue-slicksort拖拽组件
vue-slicksort拖拽组件 安装 通过npm安装 $ npm install vue-slicksort --save 通过yarn安装 $ yarn add vue-slicksort 插件 ...
随机推荐
- 关于canvas合成分享图
最近在uni-app项目中遇到一个合成分享图的需求,其实最开始是用原生写法来做的,后台发现在PC端测试是可以的,但在APP模拟器中会出现问题,可能是因为两者的js环境不同吧,uni-app官网也说了这 ...
- 第一次登陆jenkins页面空白解决方案
之前搭建了几次jenkins环境都没问题,最近换了工作,再次搭建jenkins用的是docker部署: https://www.cnblogs.com/yy-cola/p/10457484.html ...
- Spring MVC-从零开始-第一个控制器(不考虑命名规范)
1.目录结构 (log4j.properties.mybatis-config.xml可忽略) 2.配置web.xml文件 <?xml version="1.0" encod ...
- c3p0配置记录
官方文档 : http://www.mchange.com/projects/c3p0/index.html <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 ...
- jar 命令使用
1.jar命令一般用来对jar包文件处理,jar包是由JDK安装目录\bin\jar.exe命令生成的,当我们安装好JDK,设置好path路径,就可以正常使用jar.exe命令,它会用lib\tool ...
- Mysql的表级锁和行级锁
表级锁 MySQL表级锁分为读锁和写锁. 读锁 用法:LOCK TABLE table_name [ AS alias_name ] READ 释放锁使用UNLOCK tables.可以为表使用别名, ...
- HTML学习之轮播图
这可以说是一种非常简单的实现轮播图的方法了,由于时间仓促所以没写自动轮播部分.简单说一下原理吧,就是把所有图片堆叠在一个盒子里,设置所有图片的透明度为0,这样就把所有图片都隐藏了,第一张图片除外(第一 ...
- .net core 3.0 Signalr - 06 业务实现-业务分析
## 业务需求 1. 人-项目关系 一个人可以属于多个项目,一个项目可以有多个人加入,通知的时候,可以通知项目内的所有人,也可以通知部分人或者某个责任人. 2. 登录互斥 同一个人不允许登录两次(不同 ...
- 【原创】(六)Linux内存管理 - zoned page frame allocator - 1
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- Stanford公开课《编译原理》学习笔记(2)递归下降法
目录 一. Parse阶段 CFG Recursive Descent(递归下降遍历) 二. 递归下降遍历 2.1 预备知识 2.2 多行语句的处理思路 2.3 简易的文法定义 2.4 文法产生式的代 ...