u-navbar.vue 5.3 KB

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