You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.2 KiB

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