u-navbar.vue 10 KB

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