u-navbar.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. ></u-status-bar>
  15. <view
  16. class="u-navbar__content"
  17. :class="[border && 'u-border-bottom']"
  18. :style="{
  19. height: $u.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. <zui-svg-icon v-if="leftIcon&&leftIcon==='arrow-left'" width="48rpx" height="50rpx" icon="back" :color="leftIconColor"></zui-svg-icon>
  31. <u-icon
  32. v-if="leftIcon&&leftIcon!=='arrow-left'"
  33. :name="leftIcon"
  34. :size="leftIconSize"
  35. :color="leftIconColor"
  36. ></u-icon>
  37. <text
  38. v-if="leftText"
  39. :style="[{
  40. color: leftIconColor,
  41. fontSize: '40rpx',
  42. fontWeight: 'bold',
  43. marginLeft: '30rpx',
  44. paddingBottom: '14rpx',
  45. }, $u.addStyle(leftTextStyle)]"
  46. class="u-navbar__content__left__text"
  47. >{{ leftText }}</text>
  48. </slot>
  49. </view>
  50. <slot name="center">
  51. <text
  52. class="u-line-1 u-navbar__content__title"
  53. :style="[{
  54. width: $u.addUnit(titleWidth),
  55. }, $u.addStyle(titleStyle)]"
  56. >{{ title }}</text>
  57. </slot>
  58. <view
  59. class="u-navbar__content__right"
  60. v-if="$slots.right || rightIcon || rightText"
  61. @tap="rightClick"
  62. >
  63. <slot name="right">
  64. <u-icon
  65. v-if="rightIcon"
  66. :name="rightIcon"
  67. size="20"
  68. ></u-icon>
  69. <text
  70. v-if="rightText"
  71. class="u-navbar__content__right__text"
  72. >{{ rightText }}</text>
  73. </slot>
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. import props from './props.js';
  81. /**
  82. * Navbar 自定义导航栏
  83. * @description 此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uni-app带的导航栏。
  84. * @tutorial https://www.uviewui.com/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自带的图标 (默认 'arrow-left' )
  90. * @property {String} leftText 左边的提示文字
  91. * @property {String} rightText 右边的提示文字
  92. * @property {String} rightIcon 右边返回图标的名称,只能为uView自带的图标
  93. * @property {String} title 导航栏标题,如设置为空字符,将会隐藏标题占位区域
  94. * @property {String} bgColor 导航栏背景设置 (默认 '#ffffff' )
  95. * @property {String | Number} titleWidth 导航栏标题的最大宽度,内容超出会以省略号隐藏 (默认 '400rpx' )
  96. * @property {String | Number} height 导航栏高度(不包括状态栏高度在内,内部自动加上)(默认 '44px' )
  97. * @property {String | Number} leftIconSize 左侧返回图标的大小(默认 20px )
  98. * @property {String | Number} leftIconColor 左侧返回图标的颜色(默认 #303133 )
  99. * @property {Boolean} autoBack 点击左侧区域(返回图标),是否自动返回上一页(默认 false )
  100. * @property {Object | String} titleStyle 标题的样式,对象或字符串
  101. * @event {Function} leftClick 点击左侧区域
  102. * @event {Function} rightClick 点击右侧区域
  103. * @example <u-navbar title="剑未配妥,出门已是江湖" left-text="返回" right-text="帮助" @click-left="onClickBack" @click-right="onClickRight"></u-navbar>
  104. */
  105. export default {
  106. name: 'u-navbar',
  107. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  108. data() {
  109. return {
  110. }
  111. },
  112. methods: {
  113. // 点击左侧区域
  114. leftClick() {
  115. // 如果配置了autoBack,自动返回上一页
  116. this.$emit('leftClick')
  117. if(this.autoBack) {
  118. const pages = getCurrentPages();
  119. if (pages.length === 1) {
  120. uni.switchTab({
  121. url: '/pages/index/index'
  122. });
  123. } else {
  124. uni.navigateBack();
  125. }
  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: 999999;
  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>