u-navbar.vue 5.6 KB

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