cl-topbar.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="cl-topbar" :class="[classList]" :style="{
  3. 'padding-top': top,
  4. 'background-color': backgroundColor,
  5. color,
  6. zIndex
  7. }">
  8. <view class="cl-topbar__content" :style="{
  9. color
  10. }">
  11. <slot>
  12. <view class="cl-topbar__text" @tap="tapText">
  13. <text class="cl-topbar__title" v-if="title">{{ title }}</text>
  14. <text class="cl-topbar__description" v-if="description">{{ description }}</text>
  15. </view>
  16. </slot>
  17. </view>
  18. <view class="cl-topbar__prepend" :style="{
  19. top
  20. }">
  21. <view class="cl-topbar__icon" v-if="showBack" @tap="back">
  22. <text class="cl-icon-arrow-left"></text>
  23. </view>
  24. <slot name="prepend"></slot>
  25. </view>
  26. <view class="cl-topbar__append" :style="{
  27. top
  28. }">
  29. <slot name="append"></slot>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. const {
  35. platform,
  36. statusBarHeight
  37. } = uni.getSystemInfoSync();
  38. /**
  39. * topbar 顶部导航
  40. * @description 自定义顶部导航,弥补原生的不足
  41. * @tutorial https://docs.cool-js.com/uni/components/nav/topbar.html
  42. * @property {String} title 绑定值
  43. * @property {String} description 描述
  44. * @property {Boolean} border 是否带有下边框,默认true
  45. * @property {Boolean} showBack 是否显示返回按钮
  46. * @property {String} backgroundColor 背景颜色,默认#fff
  47. * @property {String} color 字体颜色,默认#000
  48. * @property {String} backPath 返回路径
  49. * @property {Boolean} fixed 是否固定布局在顶部
  50. * @property {Boolean} isTop 是否添加顶部安全区域
  51. * @property {Boolean} sticky 是否吸顶
  52. * @property {Number} zIndex 层级
  53. * @example <cl-topbar title="COOL-UNI" />
  54. */
  55. export default {
  56. name: "cl-topbar",
  57. props: {
  58. // 标题
  59. title: String,
  60. // 描述
  61. description: String,
  62. // 是否带有下边框
  63. border: {
  64. type: Boolean,
  65. default: false
  66. },
  67. // 是否显示返回按钮
  68. showBack: {
  69. type: Boolean,
  70. default: true
  71. },
  72. // 背景颜色
  73. backgroundColor: {
  74. type: String,
  75. default: "#fff"
  76. },
  77. // 字体颜色
  78. color: {
  79. type: String,
  80. default: "#000"
  81. },
  82. // 返回路径
  83. backPath: String,
  84. // 是否固定布局在顶部
  85. fixed: Boolean,
  86. // 是否添加顶部安全区域
  87. isTop: {
  88. type: Boolean,
  89. default: true
  90. },
  91. sticky: Boolean,
  92. zIndex: {
  93. type: Number,
  94. default: 9
  95. }
  96. },
  97. computed: {
  98. top() {
  99. console.log(this.isTop,'----------------',statusBarHeight,platform === "android");
  100. return this.isTop ?
  101. platform === "android" ?
  102. `${statusBarHeight}px` :
  103. "env(safe-area-inset-top)" :
  104. 0;
  105. },
  106. classList() {
  107. let list = [];
  108. if (this.border) {
  109. list.push("is-border");
  110. }
  111. if (this.fixed) {
  112. list.push("is-fixed");
  113. }
  114. if (this.sticky) {
  115. list.push("is-sticky");
  116. }
  117. return list.join(" ");
  118. }
  119. },
  120. methods: {
  121. back() {
  122. let pages = getCurrentPages();
  123. if (pages.length == 1) {
  124. // H5页面刷新或者分享页时,页面栈长度只有1。此时逐个验证返回的页面路径
  125. uni.reLaunch({
  126. url: this.backPath || this.$cl.homePage || this.$store.state.app.info.pages.home
  127. });
  128. } else {
  129. uni.navigateBack({
  130. delta: 1
  131. });
  132. }
  133. },
  134. tapText(e) {
  135. this.$emit("click", e);
  136. }
  137. }
  138. };
  139. </script>