uv-navbar.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <view class="uv-navbar">
  3. <view
  4. class="uv-navbar__placeholder"
  5. v-if="fixed && placeholder"
  6. :style="{
  7. height: $uv.addUnit($uv.getPx(height) + $uv.sys().statusBarHeight,'px'),
  8. }"
  9. ></view>
  10. <view :class="[fixed && 'uv-navbar--fixed']">
  11. <image class="uv-navbar--bgimg" :src="bgColor" :mode="imgMode" v-if="isImg" :style="[bgImgStyle]"></image>
  12. <uv-status-bar
  13. v-if="safeAreaInsetTop"
  14. :bgColor="getStatusbgColor"
  15. ></uv-status-bar>
  16. <view
  17. class="uv-navbar__content"
  18. :class="[border && 'uv-border-bottom']"
  19. :style="[{
  20. height: $uv.addUnit(height)
  21. },getBgColor]"
  22. >
  23. <view
  24. class="uv-navbar__content__left"
  25. hover-class="uv-navbar__content__left--hover"
  26. hover-start-time="150"
  27. @tap="leftClick"
  28. >
  29. <slot name="left">
  30. <uv-icon
  31. v-if="leftIcon"
  32. :name="leftIcon"
  33. :size="leftIconSize"
  34. :color="leftIconColor"
  35. ></uv-icon>
  36. <text
  37. v-if="leftText"
  38. :style="{
  39. color: leftIconColor
  40. }"
  41. class="uv-navbar__content__left__text"
  42. >{{ leftText }}</text>
  43. </slot>
  44. </view>
  45. <slot name="center">
  46. <text
  47. class="uv-line-1 uv-navbar__content__title"
  48. :style="[{
  49. width: $uv.addUnit(titleWidth),
  50. flex: '0 1 auto'
  51. }, $uv.addStyle(titleStyle)]"
  52. >{{ title }}</text>
  53. </slot>
  54. <view
  55. class="uv-navbar__content__right"
  56. @tap="rightClick"
  57. >
  58. <slot name="right">
  59. <uv-icon
  60. v-if="rightIcon"
  61. :name="rightIcon"
  62. size="20"
  63. ></uv-icon>
  64. <text
  65. v-if="rightText"
  66. class="uv-navbar__content__right__text"
  67. >{{ rightText }}</text>
  68. </slot>
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. </template>
  74. <script>
  75. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  76. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  77. import props from './props.js';
  78. /**
  79. * Navbar 自定义导航栏
  80. * @description 此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uni-app带的导航栏。
  81. * @tutorial https://www.uvui.cn/components/navbar.html
  82. * @property {Boolean} safeAreaInsetTop 是否开启顶部安全区适配 (默认 true )
  83. * @property {Boolean} placeholder 固定在顶部时,是否生成一个等高元素,以防止塌陷 (默认 false )
  84. * @property {Boolean} fixed 导航栏是否固定在顶部 (默认 false )
  85. * @property {Boolean} border 导航栏底部是否显示下边框 (默认 false )
  86. * @property {String} leftIcon 左边返回图标的名称,只能为uvui自带的图标 (默认 'arrow-left' )
  87. * @property {String} leftText 左边的提示文字
  88. * @property {String} rightText 右边的提示文字
  89. * @property {String} rightIcon 右边返回图标的名称,只能为uvui自带的图标
  90. * @property {String} title 导航栏标题,如设置为空字符,将会隐藏标题占位区域
  91. * @property {String} bgColor 导航栏背景设置 (默认 '#ffffff' )
  92. * @property {String | Number} titleWidth 导航栏标题的最大宽度,内容超出会以省略号隐藏 (默认 '400rpx' )
  93. * @property {String | Number} height 导航栏高度(不包括状态栏高度在内,内部自动加上)(默认 '44px' )
  94. * @property {String | Number} leftIconSize 左侧返回图标的大小(默认 20px )
  95. * @property {String | Number} leftIconColor 左侧返回图标的颜色(默认 #303133 )
  96. * @property {Boolean} autoBack 点击左侧区域(返回图标),是否自动返回上一页(默认 false )
  97. * @property {Object | String} titleStyle 标题的样式,对象或字符串
  98. * @event {Function} leftClick 点击左侧区域
  99. * @event {Function} rightClick 点击右侧区域
  100. * @example <uv-navbar title="剑未配妥,出门已是江湖" left-text="返回" right-text="帮助" @click-left="onClickBack" @click-right="onClickRight"></uv-navbar>
  101. */
  102. export default {
  103. name: 'uv-navbar',
  104. mixins: [mpMixin, mixin, props],
  105. data() {
  106. return {
  107. }
  108. },
  109. computed: {
  110. getBgColor(){
  111. const style = {};
  112. if(this.bgColor){
  113. if (this.bgColor.indexOf("gradient") > -1) {// 渐变色
  114. style.backgroundImage = this.bgColor;
  115. }else if(this.isImg){
  116. style.background = 'transparent';
  117. }else {
  118. style.background = this.bgColor;
  119. }
  120. }
  121. return style;
  122. },
  123. getStatusbgColor() {
  124. if(this.bgColor){
  125. if(this.isImg){
  126. return 'transparent';
  127. }else {
  128. return this.bgColor;
  129. }
  130. }
  131. },
  132. // 判断传入的bgColor属性,是否图片路径,只要带有"/"均认为是图片形式
  133. isImg() {
  134. const isBase64 = this.bgColor.indexOf('data:') > -1 && this.bgColor.indexOf('base64') > -1;
  135. return this.bgColor.indexOf('/') !== -1 || isBase64;
  136. },
  137. bgImgStyle() {
  138. const style = {};
  139. if(this.safeAreaInsetTop) {
  140. style.height = this.$uv.addUnit(this.$uv.sys().statusBarHeight + 44, 'px');
  141. } else {
  142. style.height = '44px';
  143. }
  144. return style;
  145. }
  146. },
  147. methods: {
  148. // 点击左侧区域
  149. leftClick() {
  150. // 如果配置了autoBack,自动返回上一页
  151. this.$emit('leftClick')
  152. if(this.autoBack) {
  153. uni.navigateBack()
  154. }
  155. },
  156. // 点击右侧区域
  157. rightClick() {
  158. this.$emit('rightClick')
  159. }
  160. }
  161. }
  162. </script>
  163. <style lang="scss" scoped>
  164. $show-border: 1;
  165. $show-border-bottom: 1;
  166. $show-lines: 1;
  167. @import '@/uni_modules/uv-ui-tools/libs/css/variable.scss';
  168. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  169. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  170. .uv-navbar {
  171. position: relative;
  172. &--fixed {
  173. position: fixed;
  174. left: 0;
  175. right: 0;
  176. top: 0;
  177. z-index: 11;
  178. }
  179. &--bgimg {
  180. position: absolute;
  181. left: 0;
  182. right: 0;
  183. bottom: 0;
  184. top: 0;
  185. /* #ifndef APP-NVUE */
  186. width: 100%;
  187. height: 100%;
  188. /* #endif */
  189. /* #ifdef APP-NVUE */
  190. width: 750rpx;
  191. /* #endif */
  192. }
  193. &__content {
  194. @include flex(row);
  195. align-items: center;
  196. height: 44px;
  197. background-color: #9acafc;
  198. position: relative;
  199. justify-content: center;
  200. &__left,
  201. &__right {
  202. padding: 0 13px;
  203. position: absolute;
  204. top: 0;
  205. bottom: 0;
  206. @include flex(row);
  207. align-items: center;
  208. }
  209. &__left {
  210. left: 0;
  211. &--hover {
  212. opacity: 0.7;
  213. }
  214. &__text {
  215. font-size: 15px;
  216. margin-left: 3px;
  217. }
  218. }
  219. &__title {
  220. text-align: center;
  221. font-size: 16px;
  222. color: $uv-main-color;
  223. }
  224. &__right {
  225. right: 0;
  226. &__text {
  227. font-size: 15px;
  228. margin-left: 3px;
  229. }
  230. }
  231. }
  232. }
  233. </style>