微信小程序之手动写导航栏

之前我们用vant写过类似的,这次我们手写,图标用阿里的矢量图标库。

之前的,微信学习之导航功能:

https://blog.youkuaiyun.com/cau_eric/article/details/148062848?spm=1001.2014.3001.5501

用到的资源:

阿里矢量图标网站:https://www.iconfont.cn/

这次,我们要做这个:

我们先新建一个page,music

music.wxml 先添加几个vew,

如下:

<!--pages/music/music.wxml-->

<view class="navContainer">
  <view class="navItem">
    <text>图标</text>
    <text>每日推荐</text>
  </view>
  <view class="navItem">
    <text>图标</text>
    <text>歌单</text>
  </view>
  <view class="navItem">
    <text>图标</text>
    <text>排行榜</text>
  </view>
  <view class="navItem">
    <text>图标</text>
    <text>电台</text>
  </view>
  <view class="navItem">
    <text>图标</text>
    <text>直播</text>
  </view>
</view>

这时候是这样的:

 

我们要达到我们的效果呢,要写样式文件:

.navContainer {
  display: flex;
}

.navItem {
  display: flex;
  flex-direction: column;
  width: 20%;
  align-items: center;
}

这个时候大概就是我们想要的样子了:

 

这时我们去 iconfont-阿里巴巴矢量图标库 取需要的图标,把它们都加入到项目里,

我们在微信小程序工具中,在项目根目录下建立文件,且叫iconfont.wxss

把矢量图标库的图标类加入进来。

iconfont.wxss 代码:

@font-face {
  font-family: "iconfont"; /* Project id 4958113 */
  src: url('//at.alicdn.com/t/c/font_4958113_1s4cfj6f3ze.woff2?t=1750749501394') format('woff2'),
       url('//at.alicdn.com/t/c/font_4958113_1s4cfj6f3ze.woff?t=1750749501394') format('woff'),
       url('//at.alicdn.com/t/c/font_4958113_1s4cfj6f3ze.ttf?t=1750749501394') format('truetype');
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-paixingbang:before {
  content: "\e7bd";
}

.icon-meirituijian:before {
  content: "\e603";
}

.icon-zhibo:before {
  content: "\e7d5";
}

.icon-gedan:before {
  content: "\e636";
}

.icon-diantai:before {
  content: "\e605";
}

注:怎么获取这个代码?

 

 

好,接下来我们就要引入图标库:

新建 app.wxss ,代码:

@import "/static/iconfont/iconfont.wxss";

page {
  height: 100%;
}

好了,这样就可以使用了,我们把图标替换成实际的图标:

<!--pages/music/music.wxml-->

<view class="navContainer">
  <view class="navItem">
    <text class="iconfont icon-meirituijian"></text>
    <text>每日推荐</text>
  </view>
  <view class="navItem">
    <text class="iconfont icon-gedan"></text>
    <text>歌单</text>
  </view>
  <view class="navItem">
    <text class="iconfont icon-paixingbang"></text>
    <text>排行榜</text>
  </view>
  <view class="navItem">
    <text class="iconfont icon-diantai"></text>
    <text>电台</text>
  </view>
  <view class="navItem">
    <text class="iconfont icon-zhibo"></text>
    <text>直播</text>
  </view>
</view>

 

这时候是这样的:

给图标和下面的字体写个样式:

/* pages/music/music.wxss */
.navItem .iconfont {
  width: 100rpx;
  height: 100rpx;
  border-radius: 50%;
  text-align: center;
  line-height: 100rpx;
  color: #fff;
  background: rgb(240,19,19);
  font-size: 50rpx;
  margin: 20rpx 0;
}

.navItem text {
  font-size: 26rpx;
}

效果就出来了:

微信小程序自定义底部导航栏有多种实现要点和方法。 ### 功能自定义 可对底部导航栏进行多方面的自定义,如可自定义底部导航栏列表样式、每个菜单的默认和激活后的图标及文字样式;还能决定是否添加中间的大图标菜单,同时可自定义大图标的默认与激活样式;也能自定义激活动画,默认是心跳过渡动画;并且可获取底部导航栏高度并存储在 app 全局变量中,以便其他页面动态计算页面高度时使用;还可解决点击导航菜单时激活菜单不同步的问题,底部会根据是否有安全距离自动调整 [^1]。 ### 页面显示与状态处理 在每个导航栏的页面都需要单独一个 `onShow` 方法,当显示 tabbar 页面时,需要手动 `setData` 选中当前菜单的 index ,否则无法实现自定义 tabbar 的选中状态。可通过 `getTabBar` 接口获取组件实例来解决自定义 tabBar 选中时的状态变换 [^2]。 ### 代码实现示例 以下是相关的代码示例: #### studentSysTab.wxml ```xml <view class="cu-bar tabbar bg-white" style="padding-bottom: {{bottomHeight ? '1':''}}vh;"> <block wx:for="{{list}}" wx:key="index" wx:key="{{item.pagePath}}"> <view class="action text-active {{selected == index ? 'icon-path':''}}" bindtap="nav" data-path="{{item.pagePath}}" data-index="{{index}}"> <image src="{{selected == index ? item.selectedIconPath+'?'+ random : item.iconPath}}" mode="aspectFit"></image> <text style="color:{{selected === index ? selectedColor : color}}">{{item.text}}</text> </view> </block> </view> ``` 此代码用于构建导航栏的界面结构 [^3]。 #### Uniapp 配置示例 ```json "tabBar": { "custom": true, // 关键配置:启用自定义 tabBar "list": [ { "selectedIconPath": "static/icons/home-active.png", "iconPath": "static/icons/home.png", "pagePath": "pages/index/index", "text": "首页" }, { "selectedIconPath": "static/icons/add-active.png", "iconPath": "static/icons/add.png", "pagePath": "pages/add/index", "text": "记账" }, { "selectedIconPath": "static/icons/analysis-active.png", "iconPath": "static/icons/analysis.png", "pagePath": "pages/analysis/index", "text": "分析" }, { "selectedIconPath": "static/icons/mine-active.png", "iconPath": "static/icons/mine.png", "pagePath": "pages/mine/index", "text": "我的" } ] } ``` 该配置文件中通过 `"custom": true` 启用自定义 tabBar ,并定义了导航栏的相关信息 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值