午夜91福利视频,午夜成人在线观看,午夜在线视频免费观看,午夜福利短视频,精品午夜成人免费视频APP

小程序模板網

小程序自定義底部導航

發布時間(jian):2021-06-16 08:56 所屬欄目:小程序開發教程

這個方法比較簡單,雖然有一些延遲(chi),但也(ye)算是(shi)解(jie)決(jue)了一半自定義的問題。

首先在pages同級(ji)目錄下創建(jian)tabbar.wxml文件,

  


<template name="tabBar">
<view class="tab-bar" style="color: {{tabBar.color}}; background: {{tarBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}}; {{tabBar.borderStyle? (tabBar.position=='top'? 'border-bottom: solid 1px '+tabBar.borderStyle + ';' : 'border-top: solid 1px '+tabBar.borderStyle + ';') : ''}}">
<block wx:for="{{tabBar.list}}" wx:key="pagePath">
<navigator url="{{item.pagePath}}" open-type="redirect" class="{{item.clas}}" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}}">
<image src="{{item.selectedIconPath}}" wx:if="{{item.active}}" class="img"></image>
<image src="{{item.iconPath}}" wx:if="{{!item.active}}" class="img"></image>
<text>{{item.text}}</text>
</navigator>
</block>
<view class="clear"></view>
</view>
</template>

接著(zhu)修改app.wxss樣(yang)式,有很(hen)多(duo)人沒有放(fang)樣(yang)式的代碼,為了看(kan)著(zhu)改方便,我就放(fang)一下吧(ba)。

/**app.wxss**/
 
.container {
  font-size:14px;
  height: 90rpx;

}
 
 .menu-item:nth-child(1){
   font-size: 11px;
 }
.menu-item {
  width: 24%;
  float: left;
  text-align: center;
  line-height: 30rpx;
}
.menu-item2{
  border-right:none;
}
 
.img {
  width: 40rpx;
  height: 40rpx;
  display: block;
  margin: auto;
}
 
.clear {
  clear: both;
}
 

.tab-bar {
  background-color:#242630;
  position: fixed;
  border:none;
  width: 100%;
  height: 90rpx;
  /* padding: 0px 2%; */
}

在(zai)app.js文件(jian)中加入下(xia)邊這些代碼,我直接(jie)放了全部的js,還(huan)是因為懶哈哈哈

//app.js
App({

  onLaunch: function() {
    wx.getSystemInfo({
      success: res => {
        //導航高度
        this.globalData.navHeight = res.statusBarHeight + 46;
      },
      fail(err) {
        console.log(err);
      }
    })
    // 展示本地存儲能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登錄
    wx.login({
      success: res => {
        // 發送 res.code 到后臺換取 openId, sessionKey, unionId
      }
    })
    // 獲取用戶信息
    wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已經授權,可以直接調用 getUserInfo 獲取頭像昵稱,不會彈框
            wx.getUserInfo({
              success: res => {
                // 可以將 res 發送給后臺解碼出 unionId
                this.globalData.userInfo = res.userInfo

                // 由于 getUserInfo 是網絡請求,可能會在 Page.onLoad 之后才返回
                // 所以此處加入 callback 以防止這種情況
                if (this.userInfoReadyCallback) {
                  this.userInfoReadyCallback(res)
                }
              }
            })
          };
          console.log(res)
        },
      }),
      wx.getSystemInfo({
        success: res => {
          //導航高度
          this.globalData.navHeight = res.statusBarHeight + 46;
        },
        fail(err) {
          console.log(err);
        }
      })
  },
   globalData: {
    userInfo: null
  },
  editTabBar: function () {

    var _curPageArr = getCurrentPages();

    var _curPage = _curPageArr[_curPageArr.length - 1];

    var _pagePath = _curPage.__route__;

    if (_pagePath.indexOf('/') != 0) {

      _pagePath = '/' + _pagePath;

    }

    var tabBar = this.globalData.tabBar;

    for (var i = 0; i < tabBar.list.length; i++) {

      tabBar.list[i].active = false;

      if (tabBar.list[i].pagePath == _pagePath) {

        tabBar.list[i].active = true;//根據頁面地址設置當前頁面狀態

      }

    }

    _curPage.setData({

      tabBar: tabBar

    });

  },

  globalData: {

    userInfo: null,

    tabBar: {

      color: "#999999",

      selectedColor: "#FFFFFF",
      list: [
        {
          pagePath: "/pages/index/index", 
          selectedIconPath: "/pages/images/like.png",
          iconPath: "/pages/images/like.png",
          text: "首頁",
          clas: "menu-item",
          selected: false,
        },
        {

          pagePath: "/pages/line/line",

          text: "收藏",

          clas: "menu-item",

          selected: false

        },

        {

          pagePath: "/pages/shopping/shopping",

          text: "需求填寫",

          clas: "menu-item",

          selected: false

        },
        {

          pagePath: "/pages/my/my",

          text: "房屋管理",

          clas: "menu-item menu-item2",

          selected: false

        }

      ],

      position: "bottom"

    }

  }

})

到了此處所有的(de)模(mo)版(ban)代碼就已經完成(cheng)了,下面我(wo)們來引入模(mo)版(ban)

在你需要模版的.wxml文件(jian)中(zhong)加入

<view class="container">
  <import src="../../tabbar.wxml" />
  <template is="tabBar" data="{{tabBar}}" />
  2
</view>

然后(hou)再對應的js文件中加入


const app = getApp()

Page({

  data: {

  },
  onLoad: function (options) {
    app.editTabBar();
  },
})
復制代碼


易優(you)小程序(企業版)+靈活api+前后代碼開源 碼云倉(cang)庫:
本文地址://www.jinyoudianli.com/wxmini/doc/course/26502.html 復制鏈接 如需定制請聯系易優客服咨詢:

工作日 8:30-12:00 14:30-18:00
周(zhou)六及部分節(jie)假日提(ti)供值班服務

易小優
轉人工 ×