兼容h5和微信小程序自定义头部,动态设置固定定位、定位时是否占位、背景透明度、背景颜色、点击事件、是否包含系统栏、分割线、自定义样式

有更好的建议或需求欢迎指出,谢谢

<template><view><view v-if="isStatusBar" class="status-bar" :style="'height:'+statusBarHeight+'px'"><!-- 状态栏背景,设置为不透明 --></view><view v-if="isFixed && isOccupy" class="top-header" :style="'height:'+topHeight+'px'"><!-- 头部占位 --></view><view v-else-if="isStatusBar" class="top-header" :style="'height:'+statusBarHeight+'px'"><!-- 状态栏占位 --></view><!-- 头部     --><view class="nav-bar" :class="{ 'activeIsFixed': isFixed }" :style="getNavBarStyle"><view class="back" v-if="isBack" @click="back"><view class="iconfont icon-31fanhui1"></view></view><view class="left" v-else-if="!onlyCenter" @click="leftClick"><slot name="left"></slot></view><view class="center" :class="isOnlyCenter" :style="'height:'+navbarHeight+'px'"><slot></slot></view><view class="right" @click="rightClick" v-if="!onlyCenter"><slot name="right"></slot></view></view></view>
</template><script>/*** NavBar 自定义头部* @description 头部组件,主要用于头部导航* @property {Number, String} color 图标和文字颜色* @property {String} isBack 是否显示返回按钮* @property {Boolean} isFixed  是否固定顶部* @property {Boolean} onlyCenter  是否只显示中间部分* @property {String} bgColor  背景颜色* @property {Number} opacity 背景透明度* @property {Boolean} isOccupy 固定时默认占位* @property {Boolean} isStatusBar 默认包含系统状态栏* @property {Boolean} isSplitLine 头部分割线* @property {String} splitLineColor 头部分割线颜色* @property {Number} splitLineHeight 头部分割线高度* @property {String} styleSelf 自定义样式* @event {Function} back 返回* @event {Function} leftClick 左侧点击事件* @event {Function} rightClick 右侧点击事件*/export default {name: "Header",data() {return {//状态栏高度statusBarHeight: 0,//状态栏加头部高度topHeight: 0,navbarHeight: this.propNavbarHeight};},props: {// 头部高度propNavbarHeight: {type: [Number, String],default: 42},// 显示返回按钮isBack: {type: [Boolean, String],default: false},// 是否固定定位isFixed: {type: [Boolean, String],default: true},// 是否只显示中间部分onlyCenter: {type: [Boolean, String],default: false},// 背景颜色bgColor: {type: String,default: "pink"},// 背景透明度opacity: {type: [Number, String],default: 1},// 固定时默认占位isOccupy: {type: [Boolean, String],default: true},// 默认包含系统状态栏isStatusBar: {type: [Boolean, String],default: true},// 头部分割线 isSplitLine: {type: [Boolean, String],default: true},// 头部分割线颜色splitLineColor: {type: String,default: "#CCCCCC"},// 头部分割线高度splitLineHeight: {type: [Number, String],default: 1},// 字体颜色color: {type: String,default: ""},// 自定义样式styleSelf: {type: String,default: () => {return ""}}},mounted() {this.getSystemInfo()},computed: {// 中间部分样式isOnlyCenter() {return this.onlyCenter ? 'onlyCenter' : ''},// 头部样式getNavBarStyle() {let splitLine = this.isSplitLine ?`border-bottom: ${Number(this.splitLineHeight)}px solid ${this.splitLineColor};` : "";let top = this.isFixed ? `top:${Number(this.statusBarHeight)}px;` : "";let bgColor = Number(this.opacity) === 0 ? "" : `background-color:${this.bgColor};`;return `height:${Number(this.navbarHeight)}px;line-height:${Number(this.navbarHeight)}px;background:rgba(255,255,255,${Number(this.opacity)});color:${this.color};${bgColor}${top}${splitLine}${this.styleSelf}`}},methods: {// 返回back() {uni.navigateBack({})},rightClick() {this.$emit("rightClick")},leftClick() {this.$emit("leftClick")},//获取状态栏高度getSystemInfo() {uni.getSystemInfo({success: res => {this.statusBarHeight = res.statusBarHeight// #ifdef MP-WEIXIN // 让胶囊居中const custom = wx.getMenuButtonBoundingClientRect()this.navbarHeight = custom.height + (custom.top - res.statusBarHeight) * 2// #endifif (this.isStatusBar === false) this.statusBarHeight = 0this.topHeight = this.statusBarHeight + this.navbarHeight}})}}}
</script><style scoped>.status-bar {width: 100vw;position: fixed;top: 0;left: 0;right: 0;z-index: 9;background-color: #ffffff;opacity: 1}.nav-bar {display: flex;font-size: 16px;width: 100vw;}.back,.left,.right {width: 42px;height: 42px;display: flex;align-items: center;justify-content: center;}.center {flex: 1;display: flex;align-items: center;justify-content: center;}.top-header {position: relative;top: 0;width: 100%;}.activeIsFixed {position: fixed;right: 0;left: 0;z-index: 99;}.activeBgColor {border: solid 1px transparent;background-image: linear-gradient(#ffffff, #ffffff),linear-gradient(to right, #FF007F 0%, #00A0E9 100%);border-radius: 25px;background-clip: content-box, border-box;background-origin: border-box;}.onlyCenter {margin: 0 10px;}
</style>

uniapp封装自定义头部相关推荐

  1. uni-app封装自定义支付密码键盘组件及使用方法(附效果图)

    先看一下效果吧 部分代码 <!-- 使用方法 --> <uni-pay-password :show="show" @close="tapClose&q ...

  2. uniapp 封装时间选择器组件

    背景 项目使用的ui框架为uView UI,因项目功能需求需要实现时间选择器可以选择秒,但是uview不支持,所以自行解决. 最终版是基于uview UI的时间组件代码做修改,实现功能. 基于unia ...

  3. 微信小程序自定义头部导航nav

    1.封装自定义nav导航组件 // app.js App({globalData: {systeminfo: false, //系统信息headerBtnPosi: false //头部菜单高度} } ...

  4. Android uni-app 封装原生插件

    前言 据广大用户的需求,需要把我们anyRTC的SDK,封装到uni-app来使用,并且实现音视频通话.这边文章图文讲解一下怎么封装原生插件,并且在下一章uni-app实现音视频通话. anyRTC开 ...

  5. uni app 自定义 头部组件(2) 局部 右侧按钮

    上一篇为完全的自定义头部 链接:https://blog.csdn.net/qq_37403179/article/details/102955419 本片则是使用uniapp已有的自定义方法给头部添 ...

  6. 小程序中自定义头部标题栏

    小程序最开始只能全局定义头部标题栏,也就是说一旦你想某个页面自定义头部标题栏,那么所有的页面都需要重新定义(也就是所有页面都需要重写头部标题栏): 但现在新版本的微信已经开放了可以单页面自定义头部标题 ...

  7. 封装自定义的redis切库工具类ByteArrayRedisTemplate,读取byte数组反序列化成List<Object>

    封装自定义的redis切库工具类ByteArrayRedisTemplate,读取byte数组反序列化成List<Object>(使用lettuce连接池) 代码环境 框架:springb ...

  8. uniapp中自定义showToast样式

    uniapp中自定义showToast样式 使用方式 在template中引入 在需要使用show-toast的地方使用 在main.js中挂在组件 文件代码:组件show-toast show-to ...

  9. uniapp封装方法

    uniapp封装方法 参考:https://www.cnblogs.com/gqx-html/p/10967570.html 根据报错和实际情况作了一些修改 1.环境配置 common文件夹下新建co ...

最新文章

  1. 在Salesforce中处理Email的发送
  2. 斯坦福CS229机器学习课程的数学基础(线性代数)翻译完成
  3. C# lock 语法糖实现原理--《.NET Core 底层入门》之自旋锁,互斥锁,混合锁,读写锁...
  4. Java的GC机制及算法
  5. mysql创建表时出现1071_mysql 出现1071错误怎么办
  6. word中填充效果锁定纵横比_【文艺范】Word文档中的首字下沉效果
  7. 还服务器网站被k,导致网站被K的主要原因,看看你有没有中招!
  8. CodeWithMosh--mysql 学习笔记(3)
  9. SameMovie HBOMax Video Downloader for Mac如何在 Mac 上下载 HBO Max 视频?
  10. 为用户增加sudo权限(修改sudoers文件)
  11. 数仓4.0之即席查询
  12. Docker 容器中添加字体
  13. Zabbix Trapper items
  14. 赋能建筑建材企业物流网络内外联通,B2B交易管理系统打造行业智慧供应链
  15. 百度地图配合java 代码制作地图
  16. Window 电脑C盘清理
  17. 【年度总结】2022回首瞻望 | 2023大展宏“兔“
  18. java/mysql多个字段in从而保持字段一一对应
  19. 旧金山大学 计算机,旧金山大学(University of San Francisco)
  20. 三只松鼠事件竟是乌龙我们能不能给薇娅们一些耐心

热门文章

  1. 一个三流学校程序员的奋斗(勉)
  2. Windows快捷打开服务的命令查询表
  3. 向数据库插入数据没有报错,但是数据库里面并没有数据
  4. Spark GraphX Pregel 应用
  5. 虚拟机仅主机模式连接网络(RedHat7)
  6. python之Shelve使用
  7. 2023年北京.NET线下技术沙龙来了!大咖分享,还有精品好礼等你
  8. 当文学遭遇数据:《红楼梦》中的统计学错误
  9. 5款知乎大佬都说好用的软件,每一个都值得安装
  10. java 单元测试_Java程序员必须要知道的单元测试框架Junit详解