<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
最近在做的專案有這樣一個需求,也不太好描述,就是有兩個頂部選單欄,每個二級選單欄的item都有自己頁面,每個頁面都可以通過左右滑動來切換,第一個想到的實現方法就是雙層swiper巢狀,但想要達到一個聯動的效果還是有一點點複雜,去網上找了一圈也沒結果只好自己來搞一下了
先貼一下效果圖
1.先把第一層swiper框架搭好,需要能通過滑動和點選切換頁面,基本方法可百度
2.在第一層的<swiper-item>中嵌入第二層的<swiper>,方法照舊
3.基本功能能實現,問題也來了,如何實現第二層的<swiper-item>滑到盡頭時第一層的<swiper>能隨之改變,基本實現思路是通過繫結swiper元件的回撥方法bindtransition獲取swiper-item的位移資料,但是回撥的資料並沒有swiper-item的下標,所以無法判定當前滑動的swiper-item是否在邊緣,所以只能自己動腦筋了,方法就是在邊緣的swiper-item容器內加一個充滿容器的view,並且繫結touch的相關方法,在方法內設定是否越級翻頁的flag為true,當然這個flag在js中預設定義為false,有了這個flag再加上bindtransition的回撥偏移量就能夠實現越級翻頁了
4.思路上是沒問題的,但是寫完會發現有許許多多小bug,一不小心就會崩潰的那種,最後經過不斷的調整和測試,崩潰是不會了,滑動也挺順暢的,下面貼完整程式碼
wxml:
<view class="contain"> <view class='tabbar'> <view class="tabbar_item {{swipeIndex==0 ? 'on' : ''}}" data-current='0' bindtap="swichNav"> <view>item1</view> </view> <view class="tabbar_item {{swipeIndex==1 ? 'on' : ''}}" data-current='1' bindtap="swichNav"> <view>item2</view> </view> <view class="tabbar_item {{swipeIndex==2 ? 'on' : ''}}" data-current='2' bindtap="swichNav"> <view>item3</view> </view> </view> <swiper current="{{currentTab}}" class="swiper-box" duration="300" bindchange="bindChange" style="overflow-y:hidden"> <swiper-item> <view class="swiper1_top"> <view class="swiper1_top_item {{itemIndex1==0 ? 'on' : ''}}" data-current1='0' bindtap="itemSwich1">child_item1.1</view> <view class="swiper1_top_item {{itemIndex1==1 ? 'on' : ''}}" data-current1='1' bindtap="itemSwich1">child_item1.2</view> </view> <swiper current="{{itemCurrent1}}" duration="300" bindchange="swiperItemChange1" bindtransition="swiperTrans"> <swiper-item> child_item1.1的頁面 </swiper-item> <swiper-item> <view style="width:100vw;height:100%" bindtouchmove="itemTouchRightMove" bindtouchend="itemTouchRightEnd" bindtouchcancel="itemTouchRightEnd">child_item1.2的頁面</view> </swiper-item> </swiper> </swiper-item> <swiper-item> <view class="swiper1_top"> <view class="swiper1_top_item {{itemIndex2==0 ? 'on' : ''}}" data-current2='0' bindtap="itemSwich2">child_item2.1</view> <view class="swiper1_top_item {{itemIndex2==1 ? 'on' : ''}}" data-current2='1' bindtap="itemSwich2">child_item2.2</view> <view class="swiper1_top_item {{itemIndex2==2 ? 'on' : ''}}" data-current2='2' bindtap="itemSwich2">child_item2.3</view> </view> <swiper current="{{itemCurrent2}}" duration="300" bindchange="swiperItemChange2" bindtransition="swiperTrans"> <swiper-item style="width:100vw;height:100%" bindtouchmove="itemTouchLeftMove" bindtouchend="itemTouchLeftEnd" bindtouchcancel="itemTouchLeftEnd"> child_item2.1的頁面 </swiper-item> <swiper-item> <view style="width:100vw;height:100%" >child_item2.2的頁面</view> </swiper-item> <swiper-item> <view style="width:100vw;height:100%" bindtouchmove="itemTouchRightMove" bindtouchend="itemTouchRightEnd" bindtouchcancel="itemTouchRightEnd">child_item2.3的頁面</view> </swiper-item> </swiper> </swiper-item> <swiper-item> <view class="swiper1_top"> <view class="swiper1_top_item {{itemIndex3==0 ? 'on' : ''}}" data-current3='0' bindtap="itemSwich3">child_item3.1</view> <view class="swiper1_top_item {{itemIndex3==1 ? 'on' : ''}}" data-current3='1' bindtap="itemSwich3">child_item3.2</view> <view class="swiper1_top_item {{itemIndex3==2 ? 'on' : ''}}" data-current3='2' bindtap="itemSwich3">child_item3.3</view> </view> <swiper current="{{itemCurrent3}}" duration="300" bindchange="swiperItemChange3" bindtransition="swiperTrans"> <swiper-item style="width:100vw;height:100%" bindtouchmove="itemTouchLeftMove" bindtouchend="itemTouchLeftEnd" bindtouchcancel="itemTouchLeftEnd"> child_item3.1的頁面 </swiper-item> <swiper-item> <view style="width:100vw;height:100%" >child_item3.2的頁面</view> </swiper-item> <swiper-item> <view style="width:100vw;height:100%" >child_item3.3的頁面</view> </swiper-item> </swiper> </swiper-item> </swiper> </view>
wxss:
page { font-size: 3.5vw; height: 100%; width: 100%; display: flex; flex-direction: column; } swiper{ height: 100%; width: 100%; } .contain { display: flex; flex-direction: column; flex: 1; height: 0; } .tabbar { height: 5vw; width: 100vw; display: flex; flex-direction: row; justify-content: space-around; border-bottom: 3px #dbdbdb solid; padding-bottom: 2vw; } .tabbar_item { display: flex; flex: 1; flex-direction: column; align-items: center; } .on { color: coral; } .swiper-box { display: flex; flex-direction: column; flex: 1; height: 0; width: 100%; overflow-x: hidden; overflow-y: scroll; } .swiper1_top { width: 100vw; display: flex; margin-left: 2vw; flex-direction: row; font-size: 4vw; align-items: center; background-color: white; } .swiper1_top_item { flex: 1; display: flex; justify-content: center; align-items: center; padding: 2.5vw 0; } .swiper1_contain { width: 100vw; height: 100%; display: flex; flex-direction: column; align-items: center; } .swiper1_item { margin-bottom: 3vw; width: 94vw; } .dir_row { display: flex; flex-direction: row; }
js:
Page({ /** * 頁面的初始資料 */ data: { currentTab: 0, swipeIndex: 0, itemCurrent1: 0, itemIndex1: 0, itemCurrent2: 0, itemIndex2: 0, itemCurrent3: 0, itemIndex3: 0, flag1: false, flag2: false, flag3: true }, /** * 滑動切換tab */ bindChange: function(e) { console.log('debugbindcange') var that = this; that.setData({ swipeIndex: e.detail.current }); }, swiperItemChange1: function(e) { var that = this; that.setData({ itemIndex1: e.detail.current }); }, swiperItemChange2: function(e) { var that = this; that.setData({ itemIndex2: e.detail.current }); }, swiperItemChange3: function(e) { var that = this; that.setData({ itemIndex3: e.detail.current }); }, /** * 點選tab切換 */ swichNav: function(e) { var that = this; if (this.data.swipeIndex === e.currentTarget.dataset.current) { return false; } else { that.setData({ currentTab: e.currentTarget.dataset.current }) } }, itemSwich1: function(e) { var that = this; if (this.data.itemIndex1 === e.currentTarget.dataset.current1) { return false; } else { that.setData({ itemIndex1: e.currentTarget.dataset.current1, itemCurrent1: e.currentTarget.dataset.current1 }) } }, itemSwich2: function(e) { var that = this; console.log(e) if (this.data.itemIndex2 === e.currentTarget.dataset.current2) { return false; } else { that.setData({ itemIndex2: e.currentTarget.dataset.current2, itemCurrent2: e.currentTarget.dataset.current2 }) } }, itemSwich3: function(e) { var that = this; if (this.data.itemIndex3 === e.currentTarget.dataset.current3) { return false; } else { that.setData({ itemIndex3: e.currentTarget.dataset.current3, itemCurrent3: e.currentTarget.dataset.current3 }) } }, /** * 滑動item繫結事件 */ swiperTrans: function(e) { var that = this; var dx = e.detail.dx if (this.data.flag3 && (this.data.flag2) && (dx >= 50) && (dx < 100)) { console.log('debug') that.data.flag3 = false this.setData({ currentTab: that.data.swipeIndex + 1, }) } if (this.data.flag3 && (this.data.flag1) && (dx <= -50) && (dx > -100)) { that.data.flag3 = false this.setData({ currentTab: that.data.swipeIndex - 1, }) } }, itemTouchLeftMove: function(e) { this.data.flag1 = true; }, itemTouchLeftEnd: function(e) { this.data.flag1 = false; this.data.flag3 = true; }, itemTouchRightMove: function(e) { this.data.flag2 = true; }, itemTouchRightEnd: function(e) { this.data.flag2 = false; this.data.flag3 = true; } })
json沒有
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45