最初打算使用scroll-view實(shí)現(xiàn),效果好、流暢、有慣性滑動(dòng),但由于滾動(dòng)條沒(méi)法去掉、無(wú)法實(shí)現(xiàn)上下層的幀布局,最終放棄了。
創(chuàng)新互聯(lián)公司成立于2013年,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元硯山做網(wǎng)站,已為上家服務(wù),為硯山各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
還是自己寫(xiě)個(gè)吧,利用手勢(shì)事件。遺憾的是小程序中目前沒(méi)有像Android中快速滑動(dòng)事件,所以,要實(shí)現(xiàn)慣性滑動(dòng)是不可能了。
item的布局:
推薦小程序優(yōu)先使用flex布局,完全夠用。這也是微信推薦的。
{{cardTeam.name}} - 1 + ¥ 99.80 確定 刪除
主要是將刪除按鈕的設(shè)為絕對(duì)定位(position: absolute):
.item .remove{ width: 60px; height: 100%; background-color: red; position: absolute; top: 0; right: -60px; display: flex; justify-content: center; align-items: center; }
Page({ data: { cardTeams:[{"id":"aaaaa", "name":"android教程", "url":"http://www.see-source.com", "right":0, "startRight":0},{"id":"bbbb", "name":"小程序教程", "url":"http://www.see-source.com", "right":0, "startRight":0}] }, drawStart : function(e){ // console.log("drawStart"); var touch = e.touches[0]; startX = touch.clientX; startY = touch.clientY; var cardTeams = this.data.cardTeams; for(var i in cardTeams){ var data = cardTeams[i]; data.startRight = data.right; } key = true; }, drawEnd : function(e){ console.log("drawEnd"); var cardTeams = this.data.cardTeams; for(var i in cardTeams){ var data = cardTeams[i]; if(data.right <= 100/2){ data.right = 0; }else{ data.right = maxRight; } } this.setData({ cardTeams:cardTeams }); }, drawMove : function(e){ //console.log("drawMove"); var self = this; var dataId = e.currentTarget.id; var cardTeams = this.data.cardTeams; if(key){ var touch = e.touches[0]; endX = touch.clientX; endY = touch.clientY; console.log("startX="+startX+" endX="+endX ); if(endX - startX == 0) return ; var res = cardTeams; //從右往左 if((endX - startX) < 0){ for(var k in res){ var data = res[k]; if(res[k].id == dataId){ var startRight = res[k].startRight; var change = startX - endX; startRight += change; if(startRight > maxRight) startRight = maxRight; res[k].right = startRight; } } }else{//從左往右 for(var k in res){ var data = res[k]; if(res[k].id == dataId){ var startRight = res[k].startRight; var change = endX - startX; startRight -= change; if(startRight < 0) startRight = 0; res[k].right = startRight ; } } } self.setData({ cardTeams:cardTeams }); } }, //刪除item delItem: function(e){ var dataId = e.target.dataset.id; console.log("刪除"+dataId); var cardTeams = this.data.cardTeams; var newCardTeams = []; for(var i in cardTeams){ var item = cardTeams[i]; if(item.id != dataId){ newCardTeams.push(item); } } this.setData({ cardTeams:newCardTeams }); }, onLoad: function () { console.log('onLoad:'+app.globalData.domain) } })
drawStart用于記錄手指觸碰時(shí)的位置,drawMove記錄手指滑動(dòng)的位置,兩者的差值就是刪除按鈕的偏移量
drawEnd手指抬起時(shí)觸發(fā),用于設(shè)置彈回、攤開(kāi)效果。目前我的是超過(guò)一半自動(dòng)彈開(kāi),不足一半自動(dòng)收回。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。