小程序全屏選項(xiàng)卡

小程序經(jīng)常會(huì)出現(xiàn)選項(xiàng)卡和全屏選項(xiàng)卡,這里寫一下我的布局,以便日后使用。

1.創(chuàng)建選項(xiàng)卡組件

我們先創(chuàng)建一個(gè)components文件夾和navlist的組件


QQ截圖20190718114942.png
navlist的html:
<view class='nav-box'>
   <view bindtap='clicnav' data-index="{{index}}" wx:for="{{navdata}}" wx:key="{{index}}" class='nav-list {{navindex === index?"clicknav":""}}'>
       {{item.text}}
   </view>
 </view>
navlist的js:
// components/navbar/index.js
const App = getApp();

Component({
 /**
  * 組件的屬性列表
  */
 properties: {
//接收頁(yè)面?zhèn)鬟^(guò)來(lái)的選項(xiàng)卡標(biāo)簽內(nèi)容
   navdata: {
     type: Array,
     value: []
   },
//介紹swiper當(dāng)前頁(yè)面數(shù)值用于滑動(dòng)swiper時(shí)改動(dòng)選項(xiàng)卡的顯示
   current: {
     type: Number,
//當(dāng)父頁(yè)面current值改動(dòng)時(shí)觸發(fā)num方法
     observer: 'num',
   }
 },

 /**
  * 組件的初始數(shù)據(jù)
  */
 data: {
//默認(rèn)選擇第一個(gè)
     navindex:0,
 },
 attached(){
   console.log(this.data.navdata);
 },
 /**
  * 組件的方法列表
  */
 methods: {
//選項(xiàng)卡的點(diǎn)擊事件
   clicnav(e){
//index 為選項(xiàng)卡的data-index參數(shù)
     let index = e.currentTarget.dataset.index;
//賦值給navindex以改變點(diǎn)擊表簽的顏色
       this.setData({
         navindex: index
       });
 
     var that = this;
 //將值傳給父頁(yè)面,讓父頁(yè)面改變swiper的當(dāng)前輪播
     var myEventDetail = { index: index } // detail對(duì)象,提供給事件監(jiān)聽(tīng)函數(shù)
     this.triggerEvent('myevent', myEventDetail) //myevent自定義名稱事件,父組件中使用
   },

   num(num) {
   //當(dāng)父頁(yè)面current值改動(dòng)時(shí)修改navindex的值
     this.setData({
       navindex: num
     });
   }
 }
})
navlist的json:
{
  "component": true,
  "usingComponents": {}
}
navlist的css:
/* components/navlist/navlist.wxss */
.nav-box{
  width: 100%;
  height: 100rpx;
  display: flex;
}
.nav-list{
  flex: 1;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 32rpx;
  color: #000000;

  box-sizing: border-box;
  border-bottom: 2rpx solid #c4c4c4;
}
.clicknav{
  border-bottom: 2rpx solid #e74a45;
  color: #e74a45;
}

2.index頁(yè)面調(diào)用組件

index頁(yè)面調(diào)用選項(xiàng)卡組件并配合swiper滑動(dòng)

index的html:
<view id="navheight">
   <navlist bind:myevent="toggleToast" navdata="{{navdata}}" current="{{current}}"></navlist>
 </view>
   <swiper style='height:{{swiperHeight}}' current="{{navindex}}" bindchange="navchange">
     <swiper-item>
       <scroll-view style='width:100%;height:100%;background-color:green;' scroll-y>
         我是第一頁(yè)
       </scroll-view>
     </swiper-item>
     <swiper-item>
       <scroll-view style='width:100%;height:100%;background-color:yellow;' scroll-y>
         我是第二頁(yè)
       </scroll-view>
     </swiper-item>
     <swiper-item>
       <scroll-view style='width:100%;height:100%;background-color:blue;' scroll-y>
         我是第三頁(yè)
       </scroll-view>
     </swiper-item>
   </swiper>
index的js:
//index.js
//獲取應(yīng)用實(shí)例
const app = getApp()

Page({
  data: {
//傳給選項(xiàng)卡的標(biāo)簽內(nèi)容
    navdata: [{
      text: "個(gè)人信息"
    }, {
      text: "個(gè)人評(píng)價(jià)"
    }, {
      text: "項(xiàng)目歷史"
    }],
    navindex: 0,
    current: 0,
    swiperHeight: 0,
  },
  
  onLoad: function () {
    let _this = this;
    // 計(jì)算siiper到手機(jī)底部的高度賦值給swiper的height
    wx.createSelectorQuery().select('#navheight').boundingClientRect(function (rect) {
      wx.getSystemInfo({
        success(res) {
          _this.setData({
            swiperHeight: (res.windowHeight - rect.bottom) + "px"
          });
        }
      })
    }).exec()
  },
  // 獲取子組件信息
  toggleToast(e) {
    let index = e.detail.index;
    this.setData({
      navindex: index
    });
  },

  // 滑動(dòng)輪播事件
  navchange(e) {
    this.setData({
      current: e.detail.current
    });
  }
})
index的json:
{
  "usingComponents": {
    "navlist": "./components/navlist/navlist"
  }
}
index的css:無(wú)

至此我們的全屏選項(xiàng)卡就做好了

選項(xiàng)ka.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.小程序起步 (1)點(diǎn)擊https://mp.weixin.qq.com/wxopen/waregister?a...
    GXW_Lyon閱讀 3,600評(píng)論 0 0
  • 前端開(kāi)發(fā)面試題 面試題目: 根據(jù)你的等級(jí)和職位的變化,入門級(jí)到專家級(jí),廣度和深度都會(huì)有所增加。 題目類型: 理論知...
    怡寶丶閱讀 2,682評(píng)論 0 7
  • 因新工作主要負(fù)責(zé)微信小程序這一塊,最近的重心就移到這一塊,該博客是對(duì)微信小程序整體的整理歸納以及標(biāo)明一些細(xì)節(jié)點(diǎn),初...
    majun00閱讀 7,638評(píng)論 0 9
  • 目錄:碎鉆【一】碎鉆【二】碎鉆【三】碎鉆【四】碎鉆【五】碎鉆【六】碎鉆【七】碎鉆【八】碎鉆【八】碎鉆【九】碎鉆【十...
    兔兔啊兔兔吖閱讀 229評(píng)論 0 0
  • 常言說(shuō):生活中不如意有八九,如何保持好的心態(tài)呢?我的觀點(diǎn)是這么幾點(diǎn): 糊涂一點(diǎn)。經(jīng)常去裝傻,回答多...
    快樂(lè)大拙閱讀 247評(píng)論 1 2

友情鏈接更多精彩內(nèi)容