zoukankan      html  css  js  c++  java
  • 微信小程序开发之自定义菜单tabbar

    做这个 遇到问题比较多,特此记录以便查看,直接上代码:

    一、app.js

    控制原有菜单隐藏、启用新菜单、菜单列表,集中在这里控制

    hideTabBar这个很关键,解决苹果6S导致的双导航栏:原文https://blog.csdn.net/czp555/article/details/87258301

    hideTabBar: function () {
        wx.hideTabBar({
          fail: function () {
            setTimeout(function () {
              wx.hideTabBar()
            }, 500)
          }
        });
      },
      editTabbar: function() {
        let tabbar = this.globalData.tabBar;
        let currentPages = getCurrentPages();
        let _this = currentPages[currentPages.length - 1];
        let pagePath = _this.route;
        (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
        for (let i in tabbar.list) {
          tabbar.list[i].selected = false;
          (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
        }
        _this.setData({
          tabbar: tabbar
        });
      },
      globalData: { 
        tabBar: {
          "color": "#f3f3f3",
          "selectedColor": "#f3f3f3",
          "borderStyle": "white",
          "backgroundColor": "#ff731d",
          "list": [{
              "pagePath": "/youfan_market/pages/index/index",
              "text": "首页",
              "iconPath": "/youfan_market/resource/images/tabBar/home.png",
              "selectedIconPath": "/youfan_market/resource/images/tabBar/home.png"
            },
            {
              "pagePath": "/youfan_market/pages/category/category",
              "text": "分类",
              "iconPath": "/youfan_market/resource/images/tabBar/category.png",
              "selectedIconPath": "/youfan_market/resource/images/tabBar/category.png"
            },
            {
              "pagePath": "/youfan_market/pages/user/index",
              "text": "分享转发",
              "iconPath": "/youfan_market/resource/images/tabBar/share.png",
              "selectedIconPath": "/youfan_market/resource/images/tabBar/share.png",
              isShare: true
            },
            {
              "pagePath": "/youfan_market/pages/cart/cart",
              "text": "购物车",
              "iconPath": "/youfan_market/resource/images/tabBar/cart.png",
              "selectedIconPath": "/youfan_market/resource/images/tabBar/cart.png"
            },
            {
              "pagePath": "/youfan_market/pages/user/index",
              "text": "我的",
              "iconPath": "/youfan_market/resource/images/tabBar/user.png",
              "selectedIconPath": "/youfan_market/resource/images/tabBar/user.png"
            }
          ]
        }
      },

    二、自定义组件

    关于图标资源就不发了,需要的自己去iconfont找

    在小程序根目录创建组件文件夹 tabbarComponent

    tabbar.js

    // tabBarComponent/tabBar.js
    const app = getApp();
    Component({
      /**
       * 组件的属性列表
       */
      properties: {
        tabbar: {
          type: Object,
          value: {
            "backgroundColor": "#ffffff",
            "color": "#979795",
            "selectedColor": "#1c1c1b",
            "list": []
          }
        }
      }, 
      /**
       * 组件的初始数据
       */
      data: {
        isIphoneX:false // app.globalData.systemInfo.model == "iPhone X" ? true : false,
      },
    
      /**
       * 组件的方法列表
       */
      methods: { 
      }, 
    })

    tabbar.json

    {
      "component": true,
      "usingComponents": {}
    }

    tabbar.wxml

    <view class="tabbar_box {{isIphoneX?'iphoneX-height':''}}" style="background-color:{{tabbar.backgroundColor}}">
      <block wx:for="{{tabbar.list}}" wx:key="{{item.pagePath}}">
        <navigator wx:if="{{item.isSpecial}}" class="tabbar_nav" hover-class="none" url="{{item.pagePath}}" style="color:{{tabbar.selectedColor}}" open-type="navigate">
          <view class='special-wrapper'><image class="tabbar_icon" src="{{item.iconPath}}"></image></view>
          <image class='special-text-wrapper'></image> 
          {{item.text}}
        </navigator>
        <button wx:elif='{{item.isShare}}' class="tabbar_nav tabbar_btn" hover-class="none" style="color:{{tabbar.color}};border-radius:90%;" open-type="share">
          <image class="tabbar_icon" src="{{item.iconPath}}"></image> 
          {{item.text}}
        </button>
        <!-- <navigator wx:else class="tabbar_nav" hover-class="none" url="{{item.pagePath}}" style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}" open-type="redirect">
          <image class="tabbar_icon" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>
          <text>{{item.text}}</text>
        </navigator> -->
        <navigator wx:else class="tabbar_nav" hover-class="none" url="{{item.pagePath}}" style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}" open-type="switchTab">
          <image class="tabbar_icon" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image> 
          {{item.text}}
        </navigator>
      </block>
    </view>

    tabbar.wxss

    .tabbar_box {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      position: fixed;
      bottom: 0;
      left: 0;
      z-index: 999999;
       100%;
      height: 98rpx;
      box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
    }
    
    .tabbar_box.iphoneX-height {
      padding-bottom: 66rpx;
    }
    
    .middle-wrapper {
      position: absolute;
      right: 310rpx;
      bottom: 0;
      background-color: #fff;
       120rpx;
      height: 120rpx;
      border-radius: 50%;
      border-top: 2rpx solid #f2f2f3;
    }
    
    .middle-wrapper.iphoneX-height {
      bottom: 66rpx;
    }
    
    .tabbar_nav {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      font-size: 20rpx;
      height: 100%;
      position: relative;
    }
    
    .tabbar_btn {
      padding: 0;
      border: none;
      background: #ff731d;
    }
    
    .tabbar_icon {
       50rpx;
      height: 50rpx;
    }
    
    .special-wrapper {
      position: absolute;
      left: 77rpx;
      top: -36rpx;
       96rpx;
      height: 96rpx;
      border-radius: 50%;
      border-top: 2rpx solid #f2f2f3;
      background-color: #fff;
      text-align: center;
      box-sizing: border-box;
      padding: 6rpx;
    }
    
    .special-wrapper .tabbar_icon {
       84rpx;
      height: 84rpx;
    }
    
    .special-text-wrapper {
       56rpx;
      height: 56rpx;
    }

    三、页面调用

    js

    var app = getApp()
    Page({
      data: {
        tabbar: {}, 
      },
      onLoad: function(options) { 
        app.editTabbar();
        
      },
      onReady: function () {
        app.hideTabBar();
      },
      onShow: function () {
        app.hideTabBar(); 
      }, 

    json

    {
      "enablePullDownRefresh": false,
      "usingComponents": {
        "tabbar": "../../tabbarComponent/tabbar"
      }
    }

    wxml 底部添加

    <tabbar tabbar="{{tabbar}}"></tabbar>

    OK,万事大吉了

    最后要说的没源码,没时间去弄

    推荐一篇别人的写的:https://www.jianshu.com/p/2cd4a23b504b

     

  • 相关阅读:
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    National Treasures HDU
    Matrix HDU
    过山车 HDU
    Jimmy’s Assignment HDU
    Card Game Cheater HDU
    Uncle Tom's Inherited Land* HDU
    50 years, 50 colors HDU
  • 原文地址:https://www.cnblogs.com/xcsn/p/10581892.html
Copyright © 2011-2022 走看看