customNavbar.vue 5.0 KB

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