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

当然一开始想向大佬们学习一手,搜了半天,实现效果不佳,我以为我真的搞不出来了,可是就在我准备自己搞的时候发现了一个大佬写好了的组件,我就去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. Java String 类解析

    I.构造函数: public String() {} 默认构造函数 public String(String original) {} 使用原有字符串构造  public String(char va ...

  2. OpenGL在ubuntu下的成功配置

    sudo apt-get update sudo apt-get install build-essential sudo apt-get install libgl1-mesa-dev sudo a ...

  3. 解决CentOS6.x或RedHat Linux 6.x版本不能通过System eth0以固定IP访问外网的问题

    当你在VMware Workstation Pro中,打开从别人那里克隆来的系统,或者是开启迁移后的虚拟机系统时,VMware将会提示你:此虚拟机可能已被移动或 复制.为了配置特定的管理和网络功能.V ...

  4. 《菜鸟程序员成长之路:从技术小白到阿里巴巴Java工程师》

    <菜鸟程序员成长之路:从技术小白到阿里巴巴Java工程师> 国庆节快乐!一年一度长度排第二的假期终于来了. 难得有十一长假,作者也想要休息几天啦. 不管你是选择出门玩,还是在公司加班,在学 ...

  5. js深度克隆对象、数组

    function deepCopy(o) { if (o instanceof Array) { var n = []; for (var i = 0; i < o.length; ++i) { ...

  6. Flask基础(10)-->http的无状态协议解决办法一(客户端cookie)

    http的无状态协议 http是一种无状态协议,浏览器请求服务器时无状态的 什么是无状态? 无状态:指的是一次用户请求时,浏览器.服务器无法知道之前这个用户做过什么,每次请求都是一次新的请求. 无状态 ...

  7. Kotlin基本语法和使用技巧

    基本语法 val value: String? = "HelloWorld" val name: String = getName() ?: return //如果是null就re ...

  8. 〈四〉ElasticSearch的认识:基础原理的补充

    目录 想想我们漏了什么 回顾 补回 集群的建立 集群发现机制 配置文件 健康状态 补充: 小节总结 分片的管理 梳理 分片的均衡分配 主副分片的排斥 容错性: 数据路由 对于集群健康状态的影响 小节总 ...

  9. ThinkPHP5通过composer安装Workerman安装失败问题(避坑指南)

    $ composer require topthink/think-workerUsing version ^2.0 for topthink/think-worker./composer.json ...

  10. vue解决刷新时闪烁

    原文地址:原文地址 1.在vue容器的div里面加上 v-cloak <div id="app" v-cloak> 2.样式文件中加上 <style type=& ...