mp-vue拖拽组件的实现

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

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

由于写的是小程序嘛,所以用了小程序的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微信

相关文章
相关标签/搜索