u-navbar.vue 5.0 KB

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