u-navbar.vue 5.1 KB

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