You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

231 regels
6.5 KiB

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