customNavbar.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <view class="custom-navbar" :style="{'background':bgColor,'height':`${(statusBar + customBarH) * 2}rpx`}">
  3. <view class="custom-navbar-info" :style="{'background':bgColor,'position':isFixed?'fixed':'static',...customNavbarInfo }">
  4. <!-- 系统导航栏 -->
  5. <view class="system" :style="{'height':`${statusBar * 2}rpx`}"></view>
  6. <view class="navbar" :style="{'height':`${customBarH * 2}rpx`}">
  7. <view class="left" :style="{...leftStyle}">
  8. <!-- 通过具名插槽来自定义导航栏左侧内通 -->
  9. <slot name="left">
  10. <!-- 这里我就直接用文本代替了,返回图标大家自行替换哈 -->
  11. <!-- #ifdef APP-PLUS || H5 -->
  12. <view v-if="isLeft" @click="back()">
  13. <u-icon name="arrow-left" color="unset"></u-icon>
  14. {{ leftText }}
  15. </view>
  16. <!-- #endif -->
  17. <!-- 这里考虑到微信小程序分享的页面,在页面中不存在上一页面时,直接返回首页。-->
  18. <!-- #ifdef MP-WEIXIN -->
  19. <view @click="back()" v-if="isPageUp && isPageUp">返回</view>
  20. <view @click="homePage()" v-if="!isPageUp && isPageUp">首页</view>
  21. <!-- #endif -->
  22. </slot>
  23. </view>
  24. <!-- 通过具名插槽来自定义导航栏中间内通 -->
  25. <view class="content" :style="contentStyle">
  26. <slot name="content">{{ title }}</slot>
  27. </view>
  28. <!-- 通过具名插槽来自定义导航栏右侧内通 -->
  29. <view class="right">
  30. <slot name="right">
  31. </slot>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. statusBar: 0, // 状态栏
  42. customBarH: this.customBar, // 状态栏
  43. platform: "", // 平台
  44. isPageUp: true, // 是否返回上一页(微信小程序)
  45. }
  46. },
  47. props: {
  48. // 主体样式
  49. customNavbarInfo: {
  50. type: Object,
  51. default: ()=>{
  52. return {
  53. borderBottom: '1px solid #ebebeb',
  54. boxShadow: 'rgba(0, 0, 0, 0.1) 0px 1px 6px 0px'
  55. }
  56. }
  57. },
  58. // 状态栏
  59. customBar: {
  60. type: Number,
  61. default: 45
  62. },
  63. isLeft: {
  64. type: Boolean,
  65. default: true
  66. },
  67. // 主体样式
  68. leftStyle: {
  69. type: Object,
  70. default: ()=>{
  71. return {
  72. color: '#000'
  73. }
  74. }
  75. },
  76. // 自定义左侧文字
  77. leftText: {
  78. type: String,
  79. default: ""
  80. },
  81. // 标题名称
  82. title: {
  83. type: String,
  84. default: ""
  85. },
  86. // 标题名称样式
  87. contentStyle: {
  88. type: Object,
  89. default: ()=>{
  90. return {
  91. color: '#fff'
  92. }
  93. }
  94. },
  95. // 自定义右侧文字
  96. rightText: {
  97. type: String,
  98. default: ""
  99. },
  100. // 背景颜色
  101. bgColor: {
  102. type: String,
  103. default: "#fff"
  104. },
  105. // 是否固定顶部
  106. isFixed: {
  107. type: Boolean,
  108. default: false
  109. }
  110. },
  111. mounted() {
  112. /*
  113. 这里通过 uni.getSystemInfo 获取系统信息
  114. 用来计算系统导航栏高度和导航栏高度
  115. */
  116. uni.getSystemInfo({
  117. success: (res) => {
  118. this.platform = res.platform
  119. // #ifdef MP
  120. this.statusBar = res.statusBarHeight
  121. const custom = wx.getMenuButtonBoundingClientRect()
  122. this.customBarH = custom.bottom + custom.top - res.statusBarHeight
  123. // #endif
  124. // #ifdef APP-PLUS
  125. this.statusBar = res.statusBarHeight
  126. // 这里是在安卓手机上加上 3 像素(当时好像是在安卓水滴屏上,系统导航栏高度较低才加上去的,大家可以真机自己调试一下)
  127. if (res.platform == "android") {
  128. this.statusBar += 3
  129. this.customBarH += 3
  130. }
  131. // #endif
  132. }
  133. })
  134. // getCurrentPages 官方解释用于获取当前页面的实例 官方地址:https://uniapp.dcloud.net.cn/uni-app-x/api/get-current-pages.html#getcurrentpages
  135. let pages = getCurrentPages()
  136. if (pages[pages.length - 2]) {
  137. this.isPageUp = true
  138. } else {
  139. this.isPageUp = false
  140. }
  141. },
  142. methods: {
  143. // 返回上一页面
  144. back() {
  145. console.log("返回上一页!")
  146. uni.navigateBack({
  147. delta: 1
  148. })
  149. },
  150. // 微信小程序返回首页
  151. homePage() {
  152. console.log("返回首页!")
  153. uni.switchTab({
  154. url: '/pages/index/index'
  155. })
  156. }
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. .custom-navbar {
  162. width: 100vw;
  163. color: #fff;
  164. .custom-navbar-info {
  165. width: 100%;
  166. z-index: 9999;
  167. .system {
  168. width: 100%;
  169. background: transparent;
  170. }
  171. .navbar {
  172. width: 100vw;
  173. display: flex;
  174. align-items: center;
  175. justify-content: space-between;
  176. box-sizing: border-box;
  177. position: relative;
  178. padding: 0 30rpx;
  179. }
  180. .left,
  181. .right {
  182. width: 120rpx;
  183. }
  184. .left {
  185. text-align: left;
  186. font-size: 32rpx;
  187. >view {
  188. display: flex;
  189. }
  190. }
  191. .content {
  192. text-align: center;
  193. font-size: 32rpx;
  194. }
  195. .right {
  196. text-align: right;
  197. }
  198. }
  199. }
  200. </style>