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 插件 ...
随机推荐
- 这些Mysql常用命令你是否还记得?
前言 记录mysql常用命令操作 基础操作 命令行登录mysql mysql -u用户名 -p用户密码 为表增加创建时间和更新时间 ALTER TABLE order_info_tbl ADD CO ...
- mybatis中Insert后主键返回
1.Mapper的写法,返回的这个int是受影响的行号 int insertNewUser(User newUser); 2.xml的写法 <!--返回主键 形式1 --> <ins ...
- django开发后台接口error 10053/10054
初学Django,开发完接口之后访问post请求的接口遇到error10053和10054,查阅很多资料没有找到具体的原因. 在这里记录下我遇到这两个报错的原因和解决方案: get请求取请求参数:su ...
- asp.net core IdentityServer4 实现 implicit(隐式许可)实现第三方登录
前言 OAuth 2.0默认四种授权模式(GrantType) 授权码模式(authorization_code) 简化模式(implicit) 密码模式(resource owner passwor ...
- 商用hadoop集群的配置命令分布
角色 安装 hdfs配置 yarn配置 hdfs 格式化 启动yarn服务 启动hdfs服务 master yum install hadoop-hdfs-namenode yum install h ...
- Docker service endpoint with name xxx already exist问题
这是因为利用docker compose启的容器再用docker rm命令删除后,网络仍然被占用,需要手动清理 解决办法: 先用docker rm -f xxx删除容器 再输入docker netwo ...
- 详解http报文
摘要 作为一个web开发者,每天都在使用者Http协议,却总是一知半解.本文参看Http RFC7230规范,梳理了http报文部分. http 报文构成 start-line: 起始行,描述请求或响 ...
- .Net Core 商城微服务项目系列(三):Ocelot网关接入Grafana监控
使用网关之后我们面临的一个问题就是监控,我们需要知道网关的实时状态,比如当前的请求吞吐量.请求耗费的时间.请求峰值甚至需要知道具体哪个服务的哪个方法花费了多少时间.网关作为请求的中转点是监控品牌的要塞 ...
- mybatis - 通用mapper
title: 玩转spring-boot-mybatis date: 2019-03-11 19:36:57 type: "mybatis" categories: mybatis ...
- python——代理ip获取
python爬虫要经历爬虫.爬虫被限制.爬虫反限制的过程.当然后续还要网页爬虫限制优化,爬虫再反限制的一系列道高一尺魔高一丈的过程. 爬虫的初级阶段,添加headers和ip代理可以解决很多问题. 贴 ...