me-tabs.vue 5.7 KB

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