導(dǎo)讀微信小程序,簡稱小程序,英文名Mini Program,是一種不需要下載安裝即可使用的應(yīng)用,它實現(xiàn)了應(yīng)用“觸手可及”的夢想,用戶掃一掃或搜一下即可打開應(yīng)用。小程序是一種不用下載就能使用的應(yīng)用,也是一... 微信小程序,簡稱小程序,英文名Mini Program,是一種不需要下載安裝即可使用的應(yīng)用,它實現(xiàn)了應(yīng)用“觸手可及”的夢想,用戶掃一掃或搜一下即可打開應(yīng)用。小程序是一種不用下載就能使用的應(yīng)用,也是一項門檻非常高的創(chuàng)新,經(jīng)過將近兩年的發(fā)展,已經(jīng)構(gòu)造了新的小程序開發(fā)環(huán)境和開發(fā)者生態(tài)。 微信小程序中選項卡的應(yīng)用隨處可見,本篇文章就來給大家介紹微信小程序中選項卡的實現(xiàn)。思路
小程序中的swiper組件功能還是比較有限的,有待優(yōu)化。 方案1.首先在js中設(shè)置數(shù)據(jù)data: { tabs: ['菜單一', '菜單二'],// 導(dǎo)航菜單欄 curIdx:0,// 當(dāng)前導(dǎo)航索引 scrollHeight:0, //滾動高度 = 設(shè)備可視區(qū)高度 - 導(dǎo)航欄高度 list:[],// 內(nèi)容區(qū)列表 }, 在onLoad函數(shù)中填充數(shù)據(jù) /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { let list=[]; for (let i=1;i<=30;i++){ list.push(i) } this.setData({ list: list }); }, 2.在WXML中循環(huán)渲染出導(dǎo)航<!-- 導(dǎo)航欄開始 --> <view class="swiper-tab"> <view wx:for="{{tabs}}" wx:key class="swiper-tab-item {{curIdx==index?'swiper-active':''}}" data-current="{{index}}" catchtap="clickTab"> <text>{{item}}</text> </view> </view> 3.設(shè)置當(dāng)前活動導(dǎo)航樣式/*初始化樣式*/ view, text, picker, input, button, image{ display: flex; box-sizing: border-box; } /* 導(dǎo)航樣式*/ .swiper-tab { position: relative; width: 100%; height: 100rpx; justify-content: center; align-items: center; } .swiper-tab-item { background-color: #f3f3f3; width: 50%; height: 80rpx; justify-content: center; align-items: center; } .swiper-active{ background-color: rgb(129, 190, 247); color: #fff; } 4.內(nèi)容顯示區(qū)內(nèi)容顯示區(qū)使用swiper組件,swiper-item個數(shù)要與tabs數(shù)組長度 一致 <!-- 內(nèi)容開始 --> <swiper class="swiper_content" current="{{curIdx}}" bindchange="swiperTab" style='height:{{scrollHeight}}px'> <swiper-item> <scroll-view class="scroll-y" scroll-y style='height:{{scrollHeight}}px' bindscrolltolower="onReachBottom"> <view wx:for="{{list}}" wx:key> <text> 內(nèi)容一{{item}}</text> </view> </scroll-view> </swiper-item> <swiper-item> 內(nèi)容二 </swiper-item> </swiper>
onShow: function () { // 100為導(dǎo)航欄swiper-tab 的高度 this.setData({ scrollHeight: wx.getSystemInfoSync().windowHeight - (wx.getSystemInfoSync().windowWidth / 750 * 100), }) }, 5.點擊導(dǎo)航欄切換內(nèi)容//點擊切換 clickTab: function (e) { this.setData({ curIdx: e.currentTarget.dataset.current }) }, 6.滑動內(nèi)容切換導(dǎo)航欄//滑動切換 swiperTab: function (e) { this.setData({ curIdx: e.detail.current }); }, 7.可滾動區(qū)域滾動最底刷新數(shù)據(jù)/** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { // 更新列表 let list = this.data.list; console.log(list) let lens = list.length for (let i = lens; i < lens+30; i++) { list.push(i) } this.setData({ list: list }); }, 一個漂亮的選項卡就完成了。完整案例 以上就是本篇文章的全部內(nèi)容了,更多精彩請關(guān)注php中文網(wǎng)。 以上就是微信小程序中選項卡的實現(xiàn)方法的詳細內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章! 小程序是一種不需要下載安裝即可使用的應(yīng)用,它實現(xiàn)了應(yīng)用“觸手可及”的夢想,用戶掃一掃或者搜一下即可打開應(yīng)用。 |
溫馨提示:喜歡本站的話,請收藏一下本站!