当前位置: 首页 > news >正文

uniapp自定义头部(兼容微信小程序(胶囊和状态栏),兼容h5)

很早之前就写过自定义头部,但是那时偷懒写死了,现在用插槽重新写了个
有两种形式:
type1是完全自定义的,可以自己去组件改也可以用插槽改
type2是正常的返回标题和右边按钮,使用就是 title="标题" @rightClick = "函数",右边按钮是插槽,可以和下面例子一样用

效果图

类型1
在这里插入图片描述

类型2 的h5
在这里插入图片描述
类型2的小程序
在这里插入图片描述

使用

<myHead title="首页" type="2"><view slot="right"><text>提交</text></view>
</myHead>

组件代码

<template><view class="myHead"><!-- myHead的占位 --><view class="myHead_placeholder" :style="{height: addUnit(height + statusBarHeight,'px')}"></view><view class="myHead_fixed" :style="{'paddingRight': capsuleWidth + 'px'}"><!-- 状态栏 --><view class="status_bar" :style="{height: addUnit(statusBarHeight)}"></view><!-- 头部本体 --><slot><view v-if="type == 1" class="myHead_content" :style="{height: addUnit(height)}"><image src="/static/img/logo.png"></image><text>为牛只-屠宰全产业护航{{capsuleWidth}}</text></view><view v-else class="myHead_content2" :style="{height: addUnit(height)}"><view class="left" @click="leftClick"><text class="iconfont icon-left"></text></view><view class="content">{{title}}</view><view class="right" @click="rightClick"><text>提交</text></view></view></slot></view></view>
</template><script>export default {name: "myHead",props: {// 点击左侧区域(返回图标),是否自动返回上一页autoBack: {type: Boolean,default: false},title: {type: String | Number,default: '标题'},type: {type: String | Number,default: 2}},data() {return {// 状态栏statusBarHeight: 0,// 头部本体高度height: 44,// 如果有胶囊capsuleWidth: 0};},mounted() {// 手机状态栏的高度this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight || 0this.getNavBarHeight()},methods: {// 获取胶囊宽度getNavBarHeight() {// 小程序环境// #ifdef MP-WEIXINconst menuButtonInfo = uni.getMenuButtonBoundingClientRect();uni.getSystemInfo({success: (systemInfo) => {this.capsuleWidth = menuButtonInfo.width + systemInfo.screenWidth - menuButtonInfo.right; // 胶囊宽度}})// #endif// #ifdef H5this.capsuleWidth = 0// #endif},addUnit(num, unit = 'px') {return num + unit},// 点击左侧区域leftClick() {// 如果配置了autoBack,自动返回上一页this.$emit('leftClick')if (this.autoBack) {uni.navigateBack()}},// 点击右侧区域rightClick() {this.$emit('rightClick')},}}
</script><style lang="scss" scoped>.myHead {.myHead_placeholder {width: 100%;}.myHead_fixed {position: fixed;left: 0;right: 0;top: 0;z-index: 11;.myHead_content {display: flex;align-items: center;image {width: 150rpx;margin-right: 10rpx;}text {font-size: 24rpx;color: rgba(29, 37, 65, 0.5);}}.myHead_content2 {display: flex;align-items: center;justify-content: space-between;padding: 0 20rpx;.left {font-size: 28rpx;}.content {flex: 1;text-align: center;font-size: 32rpx;}.right {font-size: 28rpx;}}}}
</style>

相关文章:

  • 深度解析算法之分治(归并)
  • el-table 自定义列、自定义数据
  • 【网络编程】TCP/IP四层模型、MAC和IP
  • npm init、换源问题踩坑
  • 杰理-安卓通过map获取时间的时候,部分手机切换sbc和aac时候单耳无声音
  • redis+lua+固定窗口实现分布式限流
  • AcWing 885:求组合数 I ← 杨辉三角
  • seaborn数据统计可视化-介绍
  • 业绩回暖、股价承压,三只松鼠赴港上市能否重构价值锚点?
  • 道可云人工智能每日资讯|“人工智能科技体验展”在中国科学技术馆举行
  • GTC2025全球流量大会:领驭科技以AI云端之力,助力中国企业出海破浪前行
  • SECS-I vs HSMS-SS vs HSMS-GS 通信控制对比明细表
  • 可编程控制器应用
  • 《Go 语言高并发爬虫开发:淘宝商品 API 实时采集与 ETL 数据处理管道》
  • jenkins容器提示磁盘空间过低
  • 记一次pdf转Word的技术经历
  • pdf.js移动端预览PDF文件时,支持双指缩放
  • EasyCVR视频汇聚平台助力大型生产监控项目摄像机选型与应用
  • 多模态大型模型,实现以人为中心的精细视频理解
  • Golang|分布式索引架构
  • 企业取消“大小周”引热议,半月谈:不能将显性加班变为隐性加班
  • 卡尼领导的加拿大自由党在联邦众议院选举中获胜
  • 看展览|建造上海:1949年以来的建筑、城市与文化
  • 日韩 “打头阵”与美国贸易谈判,汽车、半导体产业忧虑重重
  • 脱发后怎么把头发养回来?脱发自救指南来了
  • 影子调查|23岁男子驾照拟注销背后的“被精神病”疑云