me-tabs.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!-- tab组件: <me-tabs v-model="tabIndex" :tabs="tabs" @change="tabChange"></me-tabs> -->
  2. <template>
  3. <view class="me-tabs" :class="{'tabs-fixed': fixed}"
  4. :style="{height: tabHeightVal, top:topFixed, 'margin-top':topMargin}">
  5. <scroll-view v-if="tabs.length" :id="viewId" :scroll-left="scrollLeft" scroll-x scroll-with-animation
  6. :scroll-animation-duration="300">
  7. <view class="tabs-item" :class="{'tabs-flex':!isScroll, 'tabs-scroll':isScroll}">
  8. <!-- tab -->
  9. <view class="tab-item" :style="{width: tabWidthVal, height: tabHeightVal, 'line-height':tabHeightVal}"
  10. v-for="(tab, i) in tabs" :class="{'active': value===i}" :key="i" @click="tabClick(i)">
  11. {{getTabName(tab)}}
  12. </view>
  13. <!-- 下划线 -->
  14. <view class="tabs-line" :style="{left:lineLeft}"></view>
  15. </view>
  16. </scroll-view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. props: {
  22. tabs: { // 支持格式: ['全部', '待付款'] 或 [{name:'全部'}, {name:'待付款'}]
  23. type: Array,
  24. default () {
  25. return []
  26. }
  27. },
  28. nameKey: { // 取name的字段
  29. type: String,
  30. default: 'name'
  31. },
  32. value: { // 当前显示的下标 (使用v-model语法糖: 1.props需为value; 2.需回调input事件)
  33. type: [String, Number],
  34. default: 0
  35. },
  36. fixed: Boolean, // 是否悬浮,默认false
  37. tabWidth: Number, // 每个tab的宽度,默认不设置值,为flex平均分配; 如果指定宽度,则不使用flex,每个tab居左,超过则水平滑动(单位默认rpx)
  38. height: { // 高度,单位rpx
  39. type: Number,
  40. default: 64
  41. },
  42. top: { // 顶部偏移的距离,默认单位rpx (当fixed=true时,已加上windowTop)
  43. type: Number,
  44. default: 0
  45. }
  46. },
  47. data() {
  48. return {
  49. viewId: 'id_' + Math.random().toString(36).substr(2, 16),
  50. scrollLeft: 0,
  51. windowWidth: 0,
  52. windowTop: 0
  53. }
  54. },
  55. computed: {
  56. isScroll() {
  57. return this.tabWidth && this.tabs.length // 指定了tabWidth的宽度,则支持水平滑动
  58. },
  59. tabHeightPx() {
  60. return uni.upx2px(this.height)
  61. },
  62. tabHeightVal() {
  63. return this.tabHeightPx + 'px'
  64. },
  65. tabWidthPx() {
  66. return uni.upx2px(this.tabWidth)
  67. },
  68. tabWidthVal() {
  69. return this.isScroll ? this.tabWidthPx + 'px' : ''
  70. },
  71. lineLeft() {
  72. if (this.isScroll) {
  73. return this.tabWidthPx * this.value + this.tabWidthPx / 2 + 'px' // 需转为px (用rpx的话iOS真机显示有误差)
  74. } else {
  75. return 100 / this.tabs.length * (this.value + 1) - 100 / (this.tabs.length * 2) + '%'
  76. }
  77. },
  78. topFixed() {
  79. return this.fixed ? this.windowTop + uni.upx2px(this.top) + 'px' : 0
  80. },
  81. topMargin() {
  82. return this.fixed ? 0 : this.top + 'rpx'
  83. }
  84. },
  85. watch: {
  86. tabs() {
  87. this.warpWidth = null; // 重新计算容器宽度
  88. this.scrollCenter(); // 水平滚动到中间
  89. },
  90. value() {
  91. this.scrollCenter(); // 水平滚动到中间
  92. }
  93. },
  94. created() {
  95. let sys = uni.getSystemInfoSync();
  96. this.windowWidth = sys.windowWidth
  97. this.windowTop = sys.windowTop
  98. },
  99. mounted() {
  100. this.scrollCenter() // 滚动到当前下标
  101. },
  102. methods: {
  103. getTabName(tab) {
  104. return typeof tab === "object" ? tab[this.nameKey] : tab
  105. },
  106. tabClick(i) {
  107. if (this.value != i) {
  108. this.$emit("input", i);
  109. this.$emit("change", i);
  110. }
  111. },
  112. async scrollCenter() {
  113. if (!this.isScroll) return;
  114. if (!this.warpWidth) { // tabs容器的宽度
  115. let rect = await this.initWarpRect()
  116. this.warpWidth = rect ? rect.width : this.windowWidth; // 某些情况下取不到宽度,暂时取屏幕宽度
  117. }
  118. let tabLeft = this.tabWidthPx * this.value + this.tabWidthPx / 2; // 当前tab中心点到左边的距离
  119. let diff = tabLeft - this.warpWidth / 2 // 如果超过tabs容器的一半,则滚动差值
  120. this.scrollLeft = diff;
  121. // #ifdef MP-TOUTIAO
  122. this.scrollTimer && clearTimeout(this.scrollTimer)
  123. this.scrollTimer = setTimeout(() => { // 字节跳动小程序,需延时再次设置scrollLeft,否则tab切换跨度较大时不生效
  124. this.scrollLeft = Math.ceil(diff)
  125. }, 400)
  126. // #endif
  127. },
  128. initWarpRect() {
  129. return new Promise(resolve => {
  130. setTimeout(() => { // 延时确保dom已渲染, 不使用$nextclick
  131. let query = uni.createSelectorQuery();
  132. // #ifndef MP-ALIPAY
  133. query = query.in(this) // 支付宝小程序不支持in(this),而字节跳动小程序必须写in(this), 否则都取不到值
  134. // #endif
  135. query.select('#' + this.viewId).boundingClientRect(data => {
  136. resolve(data)
  137. }).exec();
  138. }, 20)
  139. })
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss">
  145. .me-tabs {
  146. position: relative;
  147. font-size: 24rpx;
  148. border-bottom: 1rpx solid #eee;
  149. box-sizing: border-box;
  150. overflow-y: hidden;
  151. background-color: #fff;
  152. &.tabs-fixed {
  153. z-index: 990;
  154. position: fixed;
  155. left: 0;
  156. width: 100%;
  157. }
  158. .tabs-item {
  159. position: relative;
  160. white-space: nowrap;
  161. padding-bottom: 30rpx; // 撑开高度,再配合me-tabs的overflow-y: hidden,以达到隐藏滚动条的目的
  162. box-sizing: border-box;
  163. padding: 0 35rpx 30rpx 35rpx;
  164. .tab-item {
  165. position: relative;
  166. text-align: center;
  167. box-sizing: border-box;
  168. font-size: 32rpx;
  169. color: #7D7D7F;
  170. margin-right: 64rpx;
  171. &.active {
  172. font-size: 36rpx;
  173. font-weight: bold;
  174. color: #000000;
  175. }
  176. &.active::after {
  177. content: '';
  178. z-index: 1;
  179. position: absolute;
  180. bottom: 0rpx; // 至少与.tabs-item的padding-bottom一致,才能保证在底部边缘
  181. width: 36rpx;
  182. height: 6rpx;
  183. border-radius: 6rpx;
  184. left: 0;
  185. right: 0;
  186. margin: auto;
  187. background: #22C572;
  188. }
  189. }
  190. }
  191. // 平分的方式显示item
  192. .tabs-flex {
  193. display: flex;
  194. // .tab-item {
  195. // flex: 1;
  196. // }
  197. }
  198. // 居左显示item,支持水平滑动
  199. .tabs-scroll {
  200. .tab-item {
  201. display: inline-block;
  202. }
  203. }
  204. // 选中tab的线
  205. // .tabs-line {
  206. // z-index: 1;
  207. // position: absolute;
  208. // bottom: 30rpx; // 至少与.tabs-item的padding-bottom一致,才能保证在底部边缘
  209. // width: 36rpx;
  210. // height: 6rpx;
  211. // transform: translateX(-50%);
  212. // border-radius: 6rpx;
  213. // transition: left .3s;
  214. // background: #22C572;
  215. // }
  216. }
  217. </style>