做爲一個效率還不錯的小前端,本身的任務作完以後真的好閒啊,千盼萬盼終於盼來了業務的新需求,他要我多加一個排序題,而後用戶經過拖拽來排序,項目經理看我是個實習生,說有點複雜作不出來就算了,我這麼閒的一我的,怎麼可能作不出來,慢慢磨也能磨出來好嗎。前端
固然一開始想向大佬們學習一手,搜了半天,實現效果不佳,我覺得我真的搞不出來了,但是就在我準備本身搞的時候發現了一個大佬寫好了的組件,我就去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微信