微信小程序之拖拽排序(代碼分享)
更新時(shí)間:2017年01月21日 11:05:26 作者:艾瑞卡
本篇文章主要分享了微信小程序拖拽排序的示例代碼。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
index.wxml
<!--index.wxml-->
<view class="container">
<view bindtap="box" class="box" >
<view disable-scroll="true" wx:for="{{content}}" bindtouchmove="move" bindtouchstart="movestart" bindtouchend="moveend" data-index="{{item.id}}" data-main="{{mainx}}" class="main {{mainx == item.id? 'mainmove':'mainend'}}" style="left:{{start.x}}px; top:{{start.y}}px">{{item.content}}</view>
</view>
</view>
index.js
//index.js
//獲取應(yīng)用實(shí)例
var app = getApp();
var x,y,x1,y1,x2,y2,index,currindex,n,yy;
var arr1 = [{content:11,id:1},{content:22,id:2},{content:33,id:3},{content:44,id:4},{content:55,id:5}];
Page({
data: {
mainx:0,
content:[{content:11,id:1},{content:22,id:2},{content:33,id:3},{content:44,id:4},{content:55,id:5}],
start:{x:0,y:0}
},
movestart:function(e){
currindex = e.target.dataset.index;
x = e.touches[0].clientX;
y = e.touches[0].clientY;
x1 = e.currentTarget.offsetLeft;
y1 = e.currentTarget.offsetTop;
},
move:function(e){
yy = e.currentTarget.offsetTop;
x2 = e.touches[0].clientX-x+x1;
y2 = e.touches[0].clientY-y+y1;
this.setData({
mainx:currindex,
opacity:0.7,
start:{x:x2,y:y2}
})
},
moveend:function(){
if(y2 != 0){
var arr = [];
for(var i=0; i<this.data.content.length; i++){
arr.push(this.data.content[i]);
}
var nx = this.data.content.length;
n=1;
for(var k=2; k<nx; k++){
if(y2>(52*(k-1)+k*2-26)){
n=k;
}
}
if(y2>(52*(nx-1)+nx*2-26)){
n = nx;
}
console.log(arr);
console.log(arr1)
arr.splice((currindex-1),1);
arr.splice((n-1),0,arr1[currindex-1]);
arr1 = [];
for(var m=0; m<this.data.content.length; m++){
console.log(arr[m]);
arr[m].id = m+1;
arr1.push(arr[m]);
}
// console.log(arr1);
this.setData({
mainx:"",
content:arr,
opacity:1
})
}
}
})
index.wxss
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}
.box{width:300px; position: relative}
.main{width: 90%; height:50px; background: #eee; border: 1px solid #ccc; margin:2px auto; text-align: center; line-height: 50px;}
.mainmove{position: absolute; opacity: 0.7}
.maind{background: #fff; border:1px dashed #efefef;}
.mainend{position: static; opacity: 1;}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
詳解微信小程序之scroll-view的flex布局問(wèn)題
這篇文章主要介紹了詳解微信小程序之scroll-view的flex布局問(wèn)題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
JS實(shí)現(xiàn)帶關(guān)閉功能的阿里媽媽網(wǎng)站頂部滑出banner工具條代碼
這篇文章主要介紹了JS實(shí)現(xiàn)帶關(guān)閉功能的阿里媽媽網(wǎng)站頂部滑出banner工具條代碼,可實(shí)現(xiàn)頂部banner窗口的浮動(dòng)顯示及關(guān)閉隱藏功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
async/await實(shí)現(xiàn)Promise.all()的方式
Promise.all() 方法接收一個(gè) promise 的 iterable 類型的輸入,并且只返回一個(gè)Promise實(shí)例,并且輸入的所有 promise 的 resolve 回調(diào)的結(jié)果是一個(gè)數(shù)組,對(duì)async/await實(shí)現(xiàn)Promise.all()相關(guān)知識(shí)感興趣的朋友一起看看吧2022-12-12
Typescript協(xié)變與逆變簡(jiǎn)單理解
深入學(xué)習(xí)TypeScript類型系統(tǒng)的話,逆變、協(xié)變、雙向協(xié)變、不變是繞不過(guò)去的概念。這些概念看起來(lái)挺高大上的,其實(shí)并不復(fù)雜,這篇文章我們就來(lái)學(xué)習(xí)下協(xié)變和逆變吧2022-10-10

