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

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>
http://www.xdnf.cn/news/2753.html

相关文章:

  • 数据展示功能界面设计与实现及终端控制界面思路(17)
  • 使用OpenCV和dlib库进行人脸关键点定位
  • 2025系统架构师---管道/过滤器架构风格
  • 待验证---Oracle 19c 在 CentOS 7 上的快速安装部署指南
  • Java生成微信小程序码及小程序短链接
  • TensorFlow深度学习框架:从入门到精通的完整指南
  • 基于Java,SSH,Vue电子商品交易二手平台仿闲鱼转转验机系统设计
  • Eureka 深度解析:从原理到部署的全场景实践
  • 喷泉码技术在现代物联网中的应用 设计
  • 组装 (DIY) 一台显示器 (4K 屏支持 4 画面分屏 PBP 1080p x4)
  • 前端基础面试题(4-8)
  • 推荐 1 款 9.3k stars 的全景式开源数据分析与可视化工具
  • Python爬虫(9)Python数据存储实战:基于pymysql的MySQL数据库操作详解
  • 疫苗接种体系进入“全生命周期”时代:公共卫生治理再提速
  • 学习记录:DAY19
  • AI在Java中的场景面试题深度解析
  • 从外卖大战看O2O新趋势:上门私厨平台系统架构设计解析
  • kalibr:相机模型
  • 8.Three.js中的 StereoCamera 立体相机详解+示例代码
  • 第十三章-PHP MySQL扩展
  • 处理对象集合,输出Map<String, Map<String, List<MyObject>>>格式数据,无序组合键处理方法
  • Android学习总结之Java篇(一)
  • 记一次pdf转Word的技术经历
  • Mioty|采用报文分割(Telegram Splitting)以提高抗干扰能力的无线通信技术
  • elasticsearch底层模块解析与实践系列
  • 基于大模型的胆总管结石全流程预测与临床应用研究报告
  • 基于 SpringBoot 与 Redis 的缓存预热案例
  • 【计算机视觉】CV实战项目-高分辨率遥感图像语义分割:High-Resolution-Remote-Sensing-Semantic-Segmentation
  • 《Vue3学习手记6》
  • 人工智能期末复习1