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 插件 ...
随机推荐
- Object的wait、notify和notifyAll
Obect的wait.notify 和 notifyAll是Object提供的同步方法,也就是所有对象都生而带来的方法,估计搞java的没有不知道这几个方法的.那么他究竟是怎么使用的呢?在此处记录一下 ...
- 百万it资源百度网盘链接分享
自己大量时间整理的优质资源,容量达3000多G,有需要的朋友可以微我,资源截图: 面试资料: 书籍类: 视频类: 以上只是部分资源,想要资源的亲请加微信咨询. 欢迎加微信咨询,请备注资源: 独乐乐不 ...
- thymeleaf 将后端绑定数据直接传递js变量
根据自我需求,thymeleaf可以直接将后端数据传递给js中进行使用,例如: 1.后端接口数据: @Controllerpublic class TestController { @RequestM ...
- CSS3 transform属性
说明: transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行移动(translate).旋转(rotate).缩放(scale)或倾斜(skew) transition属 ...
- [Mathematics][MIT 18.03] Proof of a Theory about the Solution to Second-order Linear Homogeneous Differential Equation
At first, I'd like to say thank you to MIT open courses which give me the privilege to enjoy the mos ...
- react redux 二次开发流程
在一个大项目中如何引入redux及其相关技术栈(react-redux redux-thunk redux-immutable ),已经成为react前端工程师不可或缺的技能,下面通过实现一个简单的t ...
- go语言标准库之http/template
html/template包实现了数据驱动的模板,用于生成可对抗代码注入的安全HTML输出.它提供了和text/template包相同的接口,Go语言中输出HTML的场景都应使用text/templa ...
- IDEA 学习笔记之 Java项目开发
Java项目开发: 新建模块: 添加JDK: 导入本地Jars: 从远程Maven仓库下载: 创建package: 新建类/接口/枚举等: 字体太小,改字体: Duplicate Scheme 修改编 ...
- .NET进阶篇-语言章-2-Delegate委托、Event事件
知识只有经过整理才能形成技能 整个章节分布简介请查看第一篇 内容目录 一.概述 二.解析委托知识点 1.委托本质 2.委托的使用 3.委托意义 逻辑解耦,减少重复代码 代码封装支持扩展 匿名方法和La ...
- Tomcat7.0.40注册到服务启动报错error Code 1 +connector attribute sslcertificateFile must be defined when using ssl with apr
Tomcat7.0.40 注册到服务启动遇到以下几个问题: 1.启动报错errorCode1 查看日志如下图: 解决办法: 这个是因为我的jdk版本问题,因为电脑是64位,安装的jdk是32位的所以会 ...