当前位置:网站首页>小程序自定义顶部导航栏,uni-app微信小程序自定义顶部导航栏
小程序自定义顶部导航栏,uni-app微信小程序自定义顶部导航栏
2022-07-01 02:21:00 【程序猿的秃头之路】
一个需求需要把左上角按钮,改成图标,效果如下

1、自定义顶部导航栏需要注意,具体可参考uniapp文档,查看文档

2、详细代码和配置,重点是兼容不同型号机型
2.1 page.json文件中去除原生导航栏样式 "navigationStyle": "custom"

2.2 App.vue在onShow中获取数据并存储
<script>
export default {
onShow: function() {
console.log('App Show')
uni.getSystemInfo({
success: (result) => {
// 获取手机系统的状态栏高度(不同手机的状态栏高度不同) ( 不要使用uni-app官方文档的var(--status-bar-height) 官方这个是固定的20px 不对的 )
// console.log('当前手机的状态栏高度',result.statusBarHeight)
let statusBarHeight = result.statusBarHeight + 'px'
// 获取右侧胶囊的信息 单位px
const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
//bottom: 胶囊底部距离屏幕顶部的距离
//height: 胶囊高度
//left: 胶囊左侧距离屏幕左侧的距离
//right: 胶囊右侧距离屏幕左侧的距离
//top: 胶囊顶部距离屏幕顶部的距离
//width: 胶囊宽度
// console.log(menuButtonInfo.width, menuButtonInfo.height, menuButtonInfo.top)
// console.log('计算胶囊右侧距离屏幕右边距离', result.screenWidth - menuButtonInfo.right)
let menuWidth = menuButtonInfo.width + 'px'
let menuHeight = menuButtonInfo.height + 'px'
let menuBorderRadius = menuButtonInfo.height / 2 + 'px'
let menuRight = result.screenWidth - menuButtonInfo.right + 'px'
let menuTop = menuButtonInfo.top + 'px'
let contentTop = result.statusBarHeight + 44 + 'px'
let menuInfo = {
statusBarHeight: statusBarHeight,//状态栏高度----用来给自定义导航条页面的顶部导航条设计padding-top使用:目的留出系统的状态栏区域
menuWidth: menuWidth,//右侧的胶囊宽度--用来给自定义导航条页面的左侧胶囊设置使用
menuHeight: menuHeight,//右侧的胶囊高度--用来给自定义导航条页面的左侧胶囊设置使用
menuBorderRadius: menuBorderRadius,//一半的圆角--用来给自定义导航条页面的左侧胶囊设置使用
menuRight: menuRight,//右侧的胶囊距离右侧屏幕距离--用来给自定义导航条页面的左侧胶囊设置使用
menuTop: menuTop,//右侧的胶囊顶部距离屏幕顶部的距离--用来给自定义导航条页面的左侧胶囊设置使用
contentTop: contentTop,//内容区距离页面最上方的高度--用来给自定义导航条页面的内容区定位距离使用
}
uni.setStorageSync('menuInfo', menuInfo)
},
fail: (error) => {
console.log(error)
}
})
},
}
</script>2.3 自定义顶部导航栏页面
<template>
<view class="index-page">
<!-- paddingTop不生效可换成marginTop -->
<view class="tab_title" :style="{ paddingTop: statusBarHeight}">
<!-- 左上角自定义样式 -->
<view class="menu_btn flex-betwee box-sizing"
:style="{ position: 'fixed', top: menuTop, left: menuRight, width: menuWidth, height: menuHeight, borderRadius: menuBorderRadius}">
<image class="logo" src="/static/logo.png" @click="goStepPage"></image>
<view class="tit">uniapp</view>
</view>
</view>
<view class="content_box box-sizing" :style="{marginTop:contentTop}">
内容
</view>
</view>
</template>
<script>
export default {
data() {
return {
statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,//状态栏的高度(可以设置为顶部导航条的padding-top)
menuWidth: uni.getStorageSync('menuInfo').menuWidth,
menuHeight: uni.getStorageSync('menuInfo').menuHeight,
menuBorderRadius: uni.getStorageSync('menuInfo').menuBorderRadius,
menuRight: uni.getStorageSync('menuInfo').menuRight,
menuTop: uni.getStorageSync('menuInfo').menuTop,
contentTop: uni.getStorageSync('menuInfo').contentTop,
}
},
}
</script>
<style lang="scss" scoped>
.box-sizing {
box-sizing: border-box;
}
.index-page {
width: 100vw;
height: calc(100vh - 64px); // 解决页面无内容时上下滚动问题 高度默认44px + padding-top 20px
// padding: 32rpx;
.tab_title {
width: 100%;
height: 44px !important; //这个是固定的44px(所有小程序顶部高度都是 = 44px + 手机系统状态栏高度)
line-height: 44px;
text-align: center;
// background-color: #d00;
background-color: #f8f8f8 !important;
position: fixed;
top: 0;
z-index: 9999;
color: #000;
font-weight: 500;
.menu_btn {
width: 414rpx !important;
// background-color: #ffffff; //这个是小程序默认的标题栏背景色
overflow: hidden;
// position: fixed; //行内式写了固定定位--目的是去掉下划页面一起滚动问题
.arrowleft {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-160%, -50%) !important;
-webkit-transform: translate(-160%, -50%) !important;
}
.text_box {
width: 1rpx;
height: 20px;
background-color: #ddd;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) !important;
-webkit-transform: translate(-50%, -50%) !important;
}
.home {
position: absolute;
top: 50%;
left: 50%;
transform: translate(60%, -50%) !important;
-webkit-transform: translate(60%, -50%) !important;
}
}
}
}
</style>完成,代码可直接复制使用
边栏推荐
- VirtualBox installation enhancements
- AS400 entretien d'usine
- URL和URI
- Analysis on user behavior loss of data exploration e-commerce platform
- Zero foundation self-study SQL course | window function
- SWT/ANR问题--AMS/WMS
- How to realize the scene linkage of intelligent lock, lamp and intelligent curtain motor in zhiting?
- Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。
- AS400 大廠面試
- 【毕业季·进击的技术er】--毕业到工作小结
猜你喜欢

Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)

零基础自学SQL课程 | 窗口函数

LabVIEW calculates the camera image sensor resolution and lens focal length

QML control type: tooltip

【JS】【掘金】获取关注了里不在关注者里的人

機器學習10-信念貝葉斯分類器

How does the property send a text message to the owner?

What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss

Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021

House change for agricultural products? "Disguised" house purchase subsidy!
随机推荐
[punch in questions] integrated daily 5 questions sharing (phase I)
LabVIEW计算相机图像传感器分辨率以及镜头焦距
I want to know how to open a stock account? Is it safe to open an account online?
5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?
[content of content type request header]
Short video platform development, relying on drawerlayout to achieve side sliding menu effect
VirtualBox 安装增强功能
The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
Template: globally balanced binary tree
集群方法同步执行框架 Suona
How does the property send a text message to the owner?
聚焦绿色低碳,数据中心散热进入“智能冷却”新时代
SWT/ANR问题--ANR/JE引发SWT
【2022年】江西省研究生数学建模方案、代码
(翻译)使用眉状文本提高标题点击率
SWT / anr issues - ams/wms
Leetcode (524) -- match the longest word in the dictionary by deleting letters
如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?
halcon变量窗口的图像变量不显示,重启软件和电脑都没用
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]