u-navbar.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view class="u-navbar">
  3. <view
  4. class="u-navbar__placeholder"
  5. v-if="fixed && placeholder"
  6. :style="{
  7. height: $u.addUnit($u.getPx(height) + $u.sys().statusBarHeight, 'px')
  8. }"
  9. ></view>
  10. <view :class="[fixed && 'u-navbar--fixed']">
  11. <u-status-bar v-if="safeAreaInsetTop" :bgColor="bgColor"></u-status-bar>
  12. <view
  13. class="u-navbar__content"
  14. :class="[border && 'u-border-bottom']"
  15. :style="{
  16. height: $u.addUnit(height),
  17. backgroundColor: bgColor
  18. }">
  19. <view class="u-navbar__content__left" hover-class="u-navbar__content__left--hover" hover-start-time="150" @tap="leftClick">
  20. <slot name="left">
  21. <u-icon v-if="leftIcon" :name="leftIcon" :size="leftIconSize" :color="leftIconColor"></u-icon>
  22. <text
  23. v-if="leftText"
  24. :style="{
  25. color: leftIconColor
  26. }"
  27. class="u-navbar__content__left__text">
  28. {{ leftText }}
  29. </text>
  30. </slot>
  31. </view>
  32. <slot name="center">
  33. <text
  34. class="u-line-1 u-navbar__content__title"
  35. :style="[
  36. {
  37. width: $u.addUnit(titleWidth)
  38. },
  39. $u.addStyle(titleStyle)
  40. ]">
  41. {{ title }}
  42. </text>
  43. </slot>
  44. <view class="u-navbar__content__right" v-if="$slots.right || rightIcon || rightText" @tap="rightClick">
  45. <slot name="right">
  46. <u-icon v-if="rightIcon" :name="rightIcon" size="20"></u-icon>
  47. <text v-if="rightText" class="u-navbar__content__right__text">{{ rightText }}</text>
  48. </slot>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import props from './props.js';
  56. import mpMixin from '../../libs/mixin/mpMixin.js';
  57. import mixin from '../../libs/mixin/mixin.js';
  58. /**
  59. * Navbar 自定义导航栏
  60. * @description 此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uni-app带的导航栏。
  61. * @tutorial https://ijry.github.io/uview-plus/components/navbar.html
  62. * @property {Boolean} safeAreaInsetTop 是否开启顶部安全区适配 (默认 true )
  63. * @property {Boolean} placeholder 固定在顶部时,是否生成一个等高元素,以防止塌陷 (默认 false )
  64. * @property {Boolean} fixed 导航栏是否固定在顶部 (默认 false )
  65. * @property {Boolean} border 导航栏底部是否显示下边框 (默认 false )
  66. * @property {String} leftIcon 左边返回图标的名称,只能为uView自带的图标 (默认 'arrow-left' )
  67. * @property {String} leftText 左边的提示文字
  68. * @property {String} rightText 右边的提示文字
  69. * @property {String} rightIcon 右边返回图标的名称,只能为uView自带的图标
  70. * @property {String} title 导航栏标题,如设置为空字符,将会隐藏标题占位区域
  71. * @property {String} bgColor 导航栏背景设置 (默认 '#ffffff' )
  72. * @property {String | Number} titleWidth 导航栏标题的最大宽度,内容超出会以省略号隐藏 (默认 '400rpx' )
  73. * @property {String | Number} height 导航栏高度(不包括状态栏高度在内,内部自动加上)(默认 '44px' )
  74. * @property {String | Number} leftIconSize 左侧返回图标的大小(默认 20px )
  75. * @property {String | Number} leftIconColor 左侧返回图标的颜色(默认 #303133 )
  76. * @property {Boolean} autoBack 点击左侧区域(返回图标),是否自动返回上一页(默认 false )
  77. * @property {Object | String} titleStyle 标题的样式,对象或字符串
  78. * @event {Function} leftClick 点击左侧区域
  79. * @event {Function} rightClick 点击右侧区域
  80. * @example <u-navbar title="剑未配妥,出门已是江湖" left-text="返回" right-text="帮助" @click-left="onClickBack" @click-right="onClickRight"></u-navbar>
  81. */
  82. export default {
  83. name: 'u-navbar',
  84. mixins: [mpMixin, mixin, props],
  85. data() {
  86. return {};
  87. },
  88. methods: {
  89. // 点击左侧区域
  90. leftClick() {
  91. // 如果配置了autoBack,自动返回上一页
  92. this.$emit('leftClick');
  93. if (this.autoBack) {
  94. uni.navigateBack();
  95. }
  96. },
  97. // 点击右侧区域
  98. rightClick() {
  99. this.$emit('rightClick');
  100. }
  101. }
  102. };
  103. </script>
  104. <style lang="scss" scoped>
  105. @import '../../libs/css/components.scss';
  106. .u-navbar {
  107. &--fixed {
  108. position: fixed;
  109. left: 0;
  110. right: 0;
  111. top: 0;
  112. z-index: 11;
  113. }
  114. &__content {
  115. @include flex(row);
  116. align-items: center;
  117. height: 44px;
  118. background-color: #9acafc;
  119. position: relative;
  120. justify-content: center;
  121. &__left,
  122. &__right {
  123. padding: 0 13px;
  124. position: absolute;
  125. top: 0;
  126. bottom: 0;
  127. @include flex(row);
  128. align-items: center;
  129. }
  130. &__left {
  131. left: 0;
  132. &--hover {
  133. opacity: 0.7;
  134. }
  135. &__text {
  136. font-size: 15px;
  137. margin-left: 3px;
  138. }
  139. }
  140. &__title {
  141. text-align: center;
  142. font-size: 16px;
  143. color: $u-main-color;
  144. }
  145. &__right {
  146. right: 0;
  147. &__text {
  148. font-size: 15px;
  149. margin-left: 3px;
  150. }
  151. }
  152. }
  153. }
  154. </style>