<template> <view :style="{paddingTop: statusBarHeight + 'px',background:bg}" class="box"> <view :style="{width: titleBarWidth + 'px', height: titleBarHeight + 'px'}"> <slot name="parent" :menuButtonHeight="menuButtonHeight" :titleBarHeight="titleBarHeight"></slot> </view> </view> </template> <script> export default { name: "titleBar", data() { return { statusBarHeight: 0, menuButtonHeight: 0, titleBarWidth: 0, titleBarHeight: 0, } }, props: ['bg'], created() { this.statusBarHeight = uni.getStorageSync("statusBarHeight"); this.menuButtonHeight = uni.getStorageSync("menuButtonHeight"); this.titleBarWidth = uni.getStorageSync("titleBarWidth"); this.titleBarHeight = uni.getStorageSync("titleBarHeight"); }, onReady() { uni.setStorageSync('statusBarHeight', uni.getSystemInfoSync().statusBarHeight); uni.setStorageSync('menuButtonHeight', uni.getMenuButtonBoundingClientRect().height); uni.setStorageSync('titleBarWidth', uni.getSystemInfoSync().windowWidth - uni.getMenuButtonBoundingClientRect() .width - (uni.getSystemInfoSync().windowWidth - uni.getMenuButtonBoundingClientRect().right) * 2); uni.setStorageSync('titleBarHeight', (uni.getMenuButtonBoundingClientRect().top - uni.getStorageSync( "statusBarHeight")) * 2 + uni.getStorageSync("menuButtonHeight")); if (!this.menuButtonHeight) { this.statusBarHeight = uni.getStorageSync("statusBarHeight"); this.menuButtonHeight = uni.getStorageSync("menuButtonHeight"); this.titleBarWidth = uni.getStorageSync("titleBarWidth"); this.titleBarHeight = uni.getStorageSync("titleBarHeight"); } } } </script> <style> .box { position: sticky; top: 0rpx; z-index: 999; } </style>
效果:
<titleBar bg="#f6f6f6"> <template v-slot:parent> <view class="xbox"> 服务内容 </view> </template> </titleBar>
.xbox{
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】