微信小程序中選項(xiàng)卡的實(shí)現(xiàn)方法

小編給大家分享一下微信小程序中選項(xiàng)卡的實(shí)現(xiàn)方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

上猶網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。

思路

  • 之前寫過基于swiper的選項(xiàng)卡,在小程序中有swiper組件,毫無疑問這里要用到swiper組件

  • 小程序中的swiper組件有個(gè)問題就是不能根據(jù)內(nèi)容自適應(yīng)高度,所以要通過wx.getSystemInfoSync獲取設(shè)備高度設(shè)置swiper高度

  • 小程序中的swiper組件中swiper-item內(nèi)容超出可視區(qū)后無法滾動(dòng)顯示,所以這里要用到另一個(gè)組件scroll-view。

小程序中的swiper組件功能還是比較有限的,有待優(yōu)化。

方案

1.首先在js中設(shè)置數(shù)據(jù)

 data: {
    tabs: ['菜單一', '菜單二'],// 導(dǎo)航菜單欄
    curIdx:0,// 當(dāng)前導(dǎo)航索引
    scrollHeight:0, //滾動(dòng)高度 = 設(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ò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個(gè)數(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>

小程序中的swiper組件有個(gè)問題就是不能根據(jù)內(nèi)容自適應(yīng)高度,所以要通過[wx.getSystemInfoSync][4]獲取設(shè)備高度設(shè)置swiper高度
小程序中的swiper組件中swiper-item內(nèi)容超出可視區(qū)后無法滾動(dòng)顯示,所以這里要用到另一個(gè)組件[scroll-view][5]。
我們在onShow函數(shù)中通過getSystemInfoSync獲取設(shè)備的寬高來設(shè)置swiper組件高度以及scroll-view高度

  onShow: function () {
    // 100為導(dǎo)航欄swiper-tab 的高度
   this.setData({
     scrollHeight: wx.getSystemInfoSync().windowHeight - (wx.getSystemInfoSync().windowWidth / 750 * 100),
   })
  },

5.點(diǎn)擊導(dǎo)航欄切換內(nèi)容

  //點(diǎn)擊切換
  clickTab: function (e) {
    this.setData({
      curIdx: e.currentTarget.dataset.current
    })
  },

6.滑動(dòng)內(nèi)容切換導(dǎo)航欄

  //滑動(dòng)切換
  swiperTab: function (e) {
    this.setData({
      curIdx: e.detail.current
    });
  },

7.可滾動(dòng)區(qū)域滾動(dòng)最底刷新數(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
    });
  
  },

看完了這篇文章,相信你對“微信小程序中選項(xiàng)卡的實(shí)現(xiàn)方法”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享題目:微信小程序中選項(xiàng)卡的實(shí)現(xiàn)方法
分享鏈接:http://bm7419.com/article4/jdccoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、做網(wǎng)站App設(shè)計(jì)、微信公眾號、Google、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
<button id="up8sz"><dl id="up8sz"></dl></button><li id="up8sz"><pre id="up8sz"></pre></li>
  • <li id="up8sz"></li>
  • <li id="up8sz"><nobr id="up8sz"><acronym id="up8sz"></acronym></nobr></li>
    <dd id="up8sz"></dd>