微信小程序自定义导航栏(模板化)微信小程序自定义导航栏(模板化)
前段时间写过一篇关于微信小程序自定义导航栏的自定义组件,但是对于分享页有一定的bug
这次用模板化又搞了一遍 优化了下Android与IOS 显示更接近微信原生的导航栏,以及修复分享页面不显示返回首页按钮
如果大家不习惯模板化的话可以 针对自己的需求拿以前封装的组件化做一些修改
微信小程序自定义导航栏(组件化)
CustomNavBar.wxml
<template name="CustomNavBar">
<block wx:if="{{ showNavBar }}">
<!-- 自定义导航栏悬浮时,卡空行 -->
<block wx:if="{{ needFixed }}">
<view style="position: relative; width: 100%; height: {{ navHeight }}px;"></view>
</block>
<view class="custom-navbar-con relative {{ iOS ? ' ios ' : ' android ' }}" style="height: {{ navHeight }}px; {{ needFixed ?
'position: fixed; top: 0;' : '' }}">
<view class="custom-navbar-statusbar-con relative" style="height: {{ statusBarHeight }}px;"></view>
<view class="custom-navbar-content relative" style="height: {{ navHeight - statusBarHeight }}px;">
<!-- 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" style="top: {{ navRightMenuRect.top - statusBarHeight - 1 }}px; left: {{
winWidth - navRightMenuRect.right }}px; height: {{ navRightMenuRect.height + 2 }}px;">
<block wx:if="{{ showBackBtn }}">
<view class="custom-navbar-icon-btn pull-left back" style="height: {{ navRightMenuRect.height }}px"
bindtap="customNavBarBack">
<image class="icon" src="http://222.178.203.72:19005/whst/63/=cnvmknZczbrcmzmds///download/../image/icon-nav-back.png" mode="aspectFit" style="width: {{ navRightMenuRect.height }}px;"
/>
<text class="text"></text>
</view>
</block>
<block wx:if="{{ showHomeBtn }}">
<view class="custom-navbar-icon-btn pull-left home" style="height: {{ navRightMenuRect.height }}px"
bindtap="customNavBarBackToHome">
<image class="icon" src="http://222.178.203.72:19005/whst/63/=cnvmknZczbrcmzmds///download/../image/icon-nav-home.png" mode="aspectFit" style="width: {{ navRightMenuRect.height }}px;"
/>
<text class="text"></text>
</view>
</block>
<!-- android端的导航栏显示方式 -->
<block wx:if="{{ navTitle && navTitle.length > 0 }}">
<view class="custom-navbar-title android pull-left" style="line-height: {{ navRightMenuRect.height - 2 }}px;">{{ 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%; }