me-tabs.vue 5.6 KB

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