uni-app自定義底部tabBar導航適配機型


1.首先需要在page.json里把

"tabBar": {
        "custom": true
}
在app.vue中讀取機型的屏幕高度 設置一個全局變量 tbBottom
 globalData: {
    //全局變量 
    tbBottom:0,
  },
 onLaunch: function () {
    let wxSync=wx.getSystemInfoSync();
this.globalData.tbBottom=wxSync.screenHeight-wxSync.safeArea.bottom
  },
然后就是組件封裝 props的selectIndex用來確定哪一個按鈕是選中的狀態
<template>
  <div class="tabbar" :style="{'bottom':tbBottom+'px'}">
    <ul>
      <li v-for="(item, index) in pageData" :key="index" @click="goPage(item.pagePath)">
        <img :src="selectIndex==index?item.selectedIconPath:item.iconPath" alt="" />
        <p>{{ item.text }}</p>
      </li>
    </ul>
  </div>
</template>
<script> export default { props:['selectIndex'], data() { return {
      tbBottom: getApp().globalData.tbBottom,
 pageData: [ { text: "首頁", pagePath:'/pages/index/index', iconPath: "/static/home.png", selectedIconPath:'/static/home_se.png' }, { text: "個人", pagePath:'/pages/service/index', iconPath: "/static/check.png", selectedIconPath:'/static/check_se.png' }, ], }; }, onLoad() {}, methods: { goPage(pagePath){ wx.switchTab({ url:pagePath, }); } }, }; </script>
<style lang="scss" scoped> .tabbar{ position: fixed; left: 0; bottom: 0; z-index: 9; width: 750rpx; height: auto; ul{ height: auto; width: 100%; display: flex; justify-content: space-between; align-items: center; li{ flex: 1; height: 120rpx; display: flex; justify-content: center; align-items: center; flex-direction: column; img{ height: 80rpx; width: 80rpx; } } } } </style>

如:在index.vue頁面中

給selectIndex傳0進去 就顯示選中第一個


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM