u-navbar.vue 5.5 KB

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