tabvs.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <view class="tab-nav-plus">
  3. <view class="tab-nav-plus-main" :class="isFixed ? 'fixed' : ''">
  4. <scroll-view :scroll-left="getLeft" :scroll-with-animation="true" :scroll-x="true" class="tab-nav-plus-scroll">
  5. <view v-for="(item,index) in tabs" :key="index" :data-index="index" @click="tabClick" class="item">
  6. <view class="tit" :class="selectIndex == index ? 'on' : ''" :style="{color: selectIndex == index ? '#FF6600': '#353535'}">{{item.name}}</view>
  7. <!-- <view class="bd" :style="{background:selectIndex == index ? tempColor: 'transparent'}"></view> -->
  8. </view>
  9. </scroll-view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default{
  15. props:{
  16. scrollTop:{
  17. type:Number,
  18. default:0,
  19. },
  20. selectIndex:{
  21. type:Number,
  22. default:0,
  23. },
  24. tabs:{
  25. type:Array,
  26. default:function(){
  27. return new Array;
  28. }
  29. }
  30. },
  31. data(){
  32. return {
  33. myTop:150,
  34. }
  35. },
  36. computed:{
  37. isFixed(){
  38. return this.scrollTop >= this.myTop;
  39. },
  40. getLeft(){ //这个计算不准确 凑合用
  41. let w = 0;
  42. for(var a in this.tabs){
  43. if(this.selectIndex < a) break;
  44. let myw = 0;
  45. if(this.selectIndex == a){
  46. myw = this.tabs[a].name.length * 32;
  47. }else{
  48. myw = this.tabs[a].name.length * 28;
  49. }
  50. w+=myw;
  51. }
  52. w+= this.selectIndex * 60;
  53. if(w > 375){
  54. return uni.upx2px(w - 375);
  55. }
  56. return 0;
  57. },
  58. },
  59. created(){
  60. setTimeout(()=>{
  61. const query = uni.createSelectorQuery().in(this);
  62. let css = '.tab-nav-plus';
  63. query.select(css).boundingClientRect(data => {
  64. this.myTop = data.top;
  65. }).exec();
  66. },500);
  67. },
  68. methods:{
  69. tabClick(e){
  70. let index = parseInt(e.currentTarget.dataset.index);
  71. this.$emit('change',index);
  72. }
  73. }
  74. }
  75. </script>
  76. <style>
  77. .tab-nav-plus{
  78. height: 80rpx;
  79. /* margin-bottom: 20upx; */
  80. }
  81. .tab-nav-plus-main{
  82. width: 100%;
  83. height: 80rpx;
  84. padding: 14rpx 30rpx 0rpx 32rpx;
  85. /* background: #FFFFFF; */
  86. /* border-top: 0rpx solid #E4E6ED; */
  87. }
  88. .tab-nav-plus-main.fixed{
  89. position: fixed;
  90. left: 0;
  91. z-index: 20;
  92. background: #FFFFFF;
  93. box-shadow:0rpx 4rpx 16rpx 0rpx rgba(0,0,0,0.08);
  94. }
  95. .tab-nav-plus-scroll{
  96. width: 100%;
  97. white-space: nowrap;
  98. height: 76rpx;
  99. }
  100. .tab-nav-plus-scroll .item{
  101. height: 60upx;
  102. line-height: 60upx;
  103. display: inline-block;
  104. margin-right: 60rpx;
  105. position: relative;
  106. }
  107. .tab-nav-plus-scroll .item:last-child{
  108. margin-right: 0;
  109. }
  110. .tab-nav-plus-scroll .item .tit{
  111. text-align: center;
  112. height: 48rpx;
  113. line-height: 48rpx;
  114. font-size: 28rpx;
  115. font-weight: 500;
  116. }
  117. .tab-nav-plus-scroll .item .tit.on{
  118. font-size: 28rpx;
  119. font-weight: 600;
  120. }
  121. .tab-nav-plus-main .tab-nav-plus-scroll .item .bd{
  122. position: absolute;
  123. z-index: 1;
  124. left: calc(50% - 18rpx);
  125. bottom: 0;
  126. width: 36rpx;
  127. height: 12rpx;
  128. border-radius: 6rpx 6rpx 0rpx 0rpx;
  129. }
  130. .tab-nav-plus-main.fixed .tab-nav-plus-scroll .item .bd{
  131. background: #FFFFFF;
  132. }
  133. </style>