WeChat applet custom bottom navigation bar component

WeChat applet custom bottom navigation bar component

This article example shares the specific implementation code of the bottom navigation bar component of the WeChat applet for your reference. The specific content is as follows

1. Create a new tabbar.vue (defined custom navigation bar component) under the file price of the public component of your own project

<template>
  <view v-if="showTabbar" class="tabbar">
    <view
      v-for="(item, index) in tabList"
      :key="index"
      class="icon"
      @click="switchTabBar(item.path, index)"
    >
      <image :src="index == current ? item.iconActivePath : item.iconPath"></image>
      <text :class="index == current ? 'active_text' : 'text'" bindtap = 'go'>{{ item.name }}</text>
    </view>
  </view>
</template>
 
<script>
  // import container from '@/channelMessage/get-container'
 
  export default {
    props: {
      showTabbar: {
        type: Boolean,
        default: true,
      },
      current:{ // current page index
    type:Number,
    default:0
   },
    },
    data() {
      return {
        selectedIndex: 0,
        tabList: [
          {
            name: "Home",
            iconPath: require("../../../static/image/img/tab-home-nor.png"),
            iconActivePath: require("../../../static/image/img/tab-home-sel.png"),
          path: "/pages/index/index",
          },
          {
            name: "Shopping Cart",
            iconPath: require("../../../static/image/img/tab-cart-nor.png"),
            iconActivePath: require("../../../static/image/img/tab-cart-sel.png"),
            path: "/pages/cart/cartEdit",
          },
          {
            name: "My",
            iconPath: require("../../../static/image/img/tab-my-nor.png"),
            iconActivePath: require("../../../static/image/img/tab-my-sel.png"),
            path: "/pages/mine/mine",
          },
        ],
    }
    },
 
    onShow() {
      // const containerId = container.getContainerId()
      // if (containerId == '1000') {
      // this.showTabbar = false
      // }
    },
    methods: {
    switchTabBar(path, index) {
      this.item_index = index
      wx.switchTab({
          url: path,
      })
        // this.$router.replace(path)
      },
    
    },
}
</script>
 
<style lang="scss" scoped>
  .tabbar {
    position: fixed;
    bottom: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 100%;
    height: 100rpx;
    background-color: #ffffff;
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
 
    .icon {
      display: flex;
      flex-direction: column;
      align-items: center;
 
      image {
        width: 50rpx;
        height: 50rpx;
      }
  }
  .active_text{
        font-size: 20rpx;
        margin-top: 5rpx;
    color: #d81e06;
      }
  .text{
    font-size: 20rpx;
    margin-top: 5rpx;
    }
  }
</style>

2. Add code to the pages.json file in the project to ensure that wx.switchTab in tabbar.vue can be used normally. The code is as follows:

"tabBar": {
    "selectedColor": "#EE2F51",
    "list": [{
      "pagePath": "pages/index/index",
      "text": "Home",
      "iconPath": "static/image/img/tab-home-nor.png",
      "selectedIconPath": "static/image/img/tab-home-sel.png"
    },{
      "pagePath": "pages/cart/cartEdit",
      "text": "Shopping Cart",
      "iconPath": "static/image/img/tab-cart-nor.png",
      "selectedIconPath": "static/image/img/tab-cart-sel.png"
    },{
      "pagePath": "pages/mine/mine",
      "text": "My",
      "iconPath": "static/image/img/tab-my-nor.png",
      "selectedIconPath": "static/image/img/tab-my-sel.png"
    }]
  },

3. Register custom components globally in main.js

import tabBar from "./customComponents/commonComponents/tabBar/index.vue";

//Change your component position, the index.vue here is the tabbar.vue mentioned earlier
Vue.component("tabBar", tabBar);

4. Introduce the registered component on the page where the navigation bar is needed

//Introduce the navigation bar component to the page <tabBar :current=item_index></tabBar>
 
//Mark status, yes the navigation bar can display different activation status according to the page data() {
      return {
        item_index: 0,
      }
}
 
//Hide WeChat's built-in navigation bar onLoad() {
      wx.hideTabBar();
},

5. Display effect

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat Mini Program Tutorial Series: Setting the Title Bar and Navigation Bar (7)
  • WeChat applet development top navigation bar example code
  • Detailed explanation of how to set the bottom navigation column of WeChat applet
  • WeChat applet custom header navigation bar and navigation bar background image navigationStyle problem
  • Implementation code of the top navigation bar in WeChat applet
  • Detailed explanation of WeChat applet capsule button return | home page custom navigation bar function
  • WeChat applet top navigation bar sliding tab effect
  • WeChat applet custom navigationBar top navigation bar adapts to all models (with complete case)
  • WeChat applet implements left sliding navigation bar
  • WeChat applet realizes the effect of navigation bar tab

<<:  HTML table markup tutorial (37): background image attribute BACKGROUND

>>:  In-depth analysis of MySQL query interception

Recommend

Example code for implementing stacked carousel effect with HTML+CSS+JS

Effect: When the slideshow moves in one direction...

Detailed examples of how to use the box-shadow property in CSS3

There are many attributes in CSS. Some attributes...

How to uninstall MySQL cleanly (tested and effective)

How to uninstall Mysql perfectly? Follow the step...

The difference between clientWidth, offsetWidth, scrollWidth in JavaScript

1. Concept They are all attributes of Element, in...

Common attacks on web front-ends and ways to prevent them

The security issues encountered in website front-...

Python3.6-MySql insert file path, the solution to lose the backslash

As shown below: As shown above, just replace it. ...

How to install MySQL 5.7.28 binary mode under CentOS 7.4

Linux system version: CentOS7.4 MySQL version: 5....

MySQL 5.7.17 installation graphic tutorial (windows)

I recently started learning database, and I feel ...

Let's talk about the performance of MySQL's COUNT(*)

Preface Basically, programmers in the workplace u...

Two ways to achieve horizontal arrangement of ul and li using CSS

Because li is a block-level element and occupies ...

In-depth study of MySQL multi-version concurrency control MVCC

MVCC MVCC (Multi-Version Concurrency Control) is ...

MySQL functional index optimization solution

When using MySQL, many developers often perform f...

Introduction to NFS service construction under Centos7

Table of contents 1. Server 2. Client 3. Testing ...