uni-events-helper-wx
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.

172 lines
4.3 KiB

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