u-navbar.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <view class="">
  3. <view class="u-navbar" :class="{ 'u-navbar-fixed': isFixed, 'u-border-bottom': borderBottom }" style="background: #Ffffff;z-index: 980;">
  4. <view class="u-status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
  5. <view class="u-navbar-inner" :style="[navbarInnerStyle]">
  6. <view class="u-back-wrap" v-if="isBack" @tap="goBack">
  7. <image src="../../../static/icon/goback.png" mode="" style="width: 64upx;height: 64upx;"></image>
  8. <!-- <view class="u-icon-wrap u-back-text u-line-1" v-if="backText" :style="[backTextStyle]">{{ backText }}</view> -->
  9. </view>
  10. <view class="u-navbar-content-title" v-if="title" :style="[titleStyle]">
  11. <view
  12. class="u-title u-line-1"
  13. >
  14. {{ title }}
  15. </view>
  16. </view>
  17. <view class="u-slot-content">
  18. <slot></slot>
  19. </view>
  20. <view class="u-slot-right">
  21. <slot name="right"></slot>
  22. </view>
  23. </view>
  24. </view>
  25. <!-- 解决fixed定位后导航栏塌陷的问题 -->
  26. <view class="u-navbar-placeholder" v-if="isFixed && !immersive" :style="{ width: '100%', height: Number(navbarHeight) + statusBarHeight + 'px' }"></view>
  27. </view>
  28. </template>
  29. <script>
  30. // 获取系统状态栏的高度
  31. let systemInfo = uni.getSystemInfoSync();
  32. let menuButtonInfo = {};
  33. // 如果是小程序,获取右上角胶囊的尺寸信息,避免导航栏右侧内容与胶囊重叠(支付宝小程序非本API,尚未兼容)
  34. // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
  35. menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  36. // #endif
  37. /**
  38. * navbar 自定义导航栏
  39. * @description 此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uniapp自带的导航栏。
  40. * @tutorial https://www.uviewui.com/components/navbar.html
  41. * @property {String Number} height 导航栏高度(不包括状态栏高度在内,内部自动加上),注意这里的单位是px(默认44)
  42. * @property {String} back-icon-color 左边返回图标的颜色(默认#606266)
  43. * @property {String} back-icon-name 左边返回图标的名称,只能为uView自带的图标(默认arrow-left)
  44. * @property {String Number} back-icon-size 左边返回图标的大小,单位rpx(默认30)
  45. * @property {String} back-text 返回图标右边的辅助提示文字
  46. * @property {Object} back-text-style 返回图标右边的辅助提示文字的样式,对象形式(默认{ color: '#606266' })
  47. * @property {String} title 导航栏标题,如设置为空字符,将会隐藏标题占位区域
  48. * @property {String Number} title-width 导航栏标题的最大宽度,内容超出会以省略号隐藏,单位rpx(默认250)
  49. * @property {String} title-color 标题的颜色(默认#606266)
  50. * @property {String Number} title-size 导航栏标题字体大小,单位rpx(默认32)
  51. * @property {Function} custom-back 自定义返回逻辑方法
  52. * @property {String Number} z-index 固定在顶部时的z-index值(默认980)
  53. * @property {Boolean} is-back 是否显示导航栏左边返回图标和辅助文字(默认true)
  54. * @property {Object} background 导航栏背景设置,见官网说明(默认{ background: '#ffffff' })
  55. * @property {Boolean} is-fixed 导航栏是否固定在顶部(默认true)
  56. * @property {Boolean} immersive 沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效(默认false)
  57. * @property {Boolean} border-bottom 导航栏底部是否显示下边框,如定义了较深的背景颜色,可取消此值(默认true)
  58. * @example <u-navbar back-text="返回" title="剑未配妥,出门已是江湖"></u-navbar>
  59. */
  60. export default {
  61. name: "u-navbar",
  62. props: {
  63. // 导航栏高度,单位px,非rpx
  64. height: {
  65. type: [String, Number],
  66. default: ''
  67. },
  68. // 返回箭头的颜色
  69. backIconColor: {
  70. type: String,
  71. default: '#606266'
  72. },
  73. // 左边返回的图标
  74. backIconName: {
  75. type: String,
  76. default: 'nav-back'
  77. },
  78. // 左边返回图标的大小,rpx
  79. backIconSize: {
  80. type: [String, Number],
  81. default: '44'
  82. },
  83. // 返回的文字提示
  84. backText: {
  85. type: String,
  86. default: ''
  87. },
  88. // 返回的文字的 样式
  89. backTextStyle: {
  90. type: Object,
  91. default () {
  92. return {
  93. color: '#606266'
  94. }
  95. }
  96. },
  97. // 导航栏标题
  98. title: {
  99. type: String,
  100. default: ''
  101. },
  102. // 标题的宽度,如果需要自定义右侧内容,且右侧内容很多时,可能需要减少这个宽度,单位rpx
  103. titleWidth: {
  104. type: [String, Number],
  105. default: '250'
  106. },
  107. // 标题的颜色
  108. titleColor: {
  109. type: String,
  110. default: '#606266'
  111. },
  112. // 标题字体是否加粗
  113. titleBold: {
  114. type: Boolean,
  115. default: false
  116. },
  117. // 标题的字体大小
  118. titleSize: {
  119. type: [String, Number],
  120. default: 32
  121. },
  122. isBack: {
  123. type: Boolean,
  124. default: ''
  125. },
  126. // 对象形式,因为用户可能定义一个纯色,或者线性渐变的颜色
  127. background: {
  128. type: Object,
  129. default () {
  130. return {
  131. background: '#ffffff'
  132. }
  133. }
  134. },
  135. // 导航栏是否固定在顶部
  136. isFixed: {
  137. type: Boolean,
  138. default: true
  139. },
  140. // 是否沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效
  141. immersive: {
  142. type: Boolean,
  143. default: false
  144. },
  145. // 是否显示导航栏的下边框
  146. borderBottom: {
  147. type: Boolean,
  148. default: true
  149. },
  150. zIndex: {
  151. type: [String, Number],
  152. default: ''
  153. },
  154. // 自定义返回逻辑
  155. customBack: {
  156. type: Function,
  157. default: null
  158. }
  159. },
  160. data() {
  161. return {
  162. menuButtonInfo: menuButtonInfo,
  163. statusBarHeight: systemInfo.statusBarHeight
  164. };
  165. },
  166. computed: {
  167. // 导航栏内部盒子的样式
  168. navbarInnerStyle() {
  169. let style = {};
  170. // 导航栏宽度,如果在小程序下,导航栏宽度为胶囊的左边到屏幕左边的距离
  171. style.height = this.navbarHeight + 'px';
  172. // // 如果是各家小程序,导航栏内部的宽度需要减少右边胶囊的宽度
  173. // #ifdef MP
  174. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  175. style.marginRight = rightButtonWidth + 'px';
  176. // #endif
  177. return style;
  178. },
  179. // 导航中间的标题的样式
  180. titleStyle() {
  181. let style = {};
  182. // #ifndef MP
  183. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  184. style.right = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  185. // #endif
  186. // #ifdef MP
  187. // 此处是为了让标题显示区域即使在小程序有右侧胶囊的情况下也能处于屏幕的中间,是通过绝对定位实现的
  188. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  189. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  190. style.right = rightButtonWidth - (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + rightButtonWidth +
  191. 'px';
  192. // #endif
  193. style.width = uni.upx2px(this.titleWidth) + 'px';
  194. return style;
  195. },
  196. // 转换字符数值为真正的数值
  197. navbarHeight() {
  198. // #ifdef APP-PLUS || H5
  199. return this.height ? this.height : 44;
  200. // #endif
  201. // #ifdef MP
  202. // 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
  203. // 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
  204. // return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
  205. let height = systemInfo.platform == 'ios' ? 44 : 48;
  206. return this.height ? this.height : height;
  207. // #endif
  208. }
  209. },
  210. created() {},
  211. methods: {
  212. goBack() {
  213. // 如果自定义了点击返回按钮的函数,则执行,否则执行返回逻辑
  214. if (typeof this.customBack === 'function') {
  215. // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
  216. // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
  217. this.customBack.bind(this.$u.$parent.call(this))();
  218. } else {
  219. uni.navigateBack();
  220. }
  221. }
  222. }
  223. };
  224. </script>
  225. <style scoped lang="scss">
  226. @import "../../libs/css/style.components.scss";
  227. .u-navbar {
  228. width: 100%;
  229. }
  230. .u-navbar-fixed {
  231. position: fixed;
  232. left: 0;
  233. right: 0;
  234. top: 0;
  235. z-index: 991;
  236. }
  237. .u-status-bar {
  238. width: 100%;
  239. }
  240. .u-navbar-inner {
  241. @include vue-flex;
  242. justify-content: space-between;
  243. position: relative;
  244. align-items: center;
  245. }
  246. .u-back-wrap {
  247. @include vue-flex;
  248. align-items: center;
  249. flex: 1;
  250. flex-grow: 0;
  251. padding: 14rpx 14rpx 14rpx 24rpx;
  252. }
  253. .u-back-text {
  254. padding-left: 4rpx;
  255. font-size: 30rpx;
  256. }
  257. .u-navbar-content-title {
  258. @include vue-flex;
  259. align-items: center;
  260. justify-content: center;
  261. flex: 1;
  262. position: absolute;
  263. left: 0;
  264. right: 0;
  265. height: 60rpx;
  266. text-align: center;
  267. flex-shrink: 0;
  268. }
  269. .u-navbar-centent-slot {
  270. flex: 1;
  271. }
  272. .u-title {
  273. line-height: 60rpx;
  274. flex: 1;
  275. font-size: 34upx;
  276. color: #333333;
  277. font-weight: 800;
  278. }
  279. .u-navbar-right {
  280. flex: 1;
  281. @include vue-flex;
  282. align-items: center;
  283. justify-content: flex-end;
  284. }
  285. .u-slot-content {
  286. flex: 1;
  287. @include vue-flex;
  288. align-items: center;
  289. }
  290. </style>