前段时间写过一篇关于微信小程序自定义导航栏的自定义组件,但是对于分享页有一定的bug
这次用模板化又搞了一遍 优化了下Android与IOS 显示更接近微信原生的导航栏,以及修复分享页面不显示返回首页按钮
如果大家不习惯模板化的话可以 针对自己的需求拿以前封装的组件化做一些修改
微信小程序自定义导航栏(组件化)
CustomNavBar.wxml
<template name="CustomNavBar"> <block wx:if="{{ showNavBar }}"> <!-- 自定义导航栏悬浮时,卡空行 --> <block wx:if="{{ needFixed }}"> <view ></view> </block> <view class="custom-navbar-con relative {{ iOS ? ' ios ' : ' android ' }}" > <view class="custom-navbar-statusbar-con relative" ></view> <view class="custom-navbar-content relative" > <!-- iOS端的导航栏显示方式 --> <block wx:if="{{ navTitle && navTitle.length > 0 }}"> <view class="custom-navbar-title ios">{{ navTitle }}</view> </block> <block wx:if="{{ showLeftMenu }}"> <view class="custom-navbar-left-menu-con clearfix" > <block wx:if="{{ showBackBtn }}"> <view class="custom-navbar-icon-btn pull-left back" bindtap="customNavBarBack"> <image class="icon" src="../../image/icon-nav-back.png" mode="aspectFit" /> <text class="text"></text> </view> </block> <block wx:if="{{ showHomeBtn }}"> <view class="custom-navbar-icon-btn pull-left home" bindtap="customNavBarBackToHome"> <image class="icon" src="../../image/icon-nav-home.png" mode="aspectFit" /> <text class="text"></text> </view> </block> <!-- android端的导航栏显示方式 --> <block wx:if="{{ navTitle && navTitle.length > 0 }}"> <view class="custom-navbar-title android pull-left" >{{ navTitle }}</view> </block> </view> </block> </view> </view> </block> </template>
CustomNavBar.wxss
.custom-navbar-con { position: relative; width: 100%; background-color: white; z-index: 9999; } .custom-navbar-con .custom-navbar-statusbar-con { width: 100%; } .custom-navbar-con .custom-navbar-content { width: 100%; } .custom-navbar-con .custom-navbar-left-menu-con { position: absolute; } .custom-navbar-con .custom-navbar-left-menu-con .custom-navbar-icon-btn { height: 100%; border-radius: 200px; border: 1px solid rgba(220, 220, 220, 0.6); } .custom-navbar-con .custom-navbar-left-menu-con .custom-navbar-icon-btn .icon { height: 90%; margin-top: 2.5%; } .custom-navbar-con .custom-navbar-left-menu-con .custom-navbar-icon-btn .text { font-size: 27rpx; } .custom-navbar-con .custom-navbar-left-menu-con .custom-navbar-icon-btn.back { border: none; } .custom-navbar-con .custom-navbar-left-menu-con .custom-navbar-icon-btn.back~.custom-navbar-icon-btn.home { margin-left: 10rpx; } .custom-navbar-con .custom-navbar-left-menu-con .custom-navbar-icon-btn.switch-shop { padding-left: 5px; padding-right: 25rpx; } .custom-navbar-con.ios .custom-navbar-title.android { display: none; } .custom-navbar-con.android .custom-navbar-title.ios { display: none; } .custom-navbar-con .custom-navbar-title.ios { font-weight: bold; text-align: center; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); } .custom-navbar-con .custom-navbar-title.android { padding-left: 30rpx; }```