tab.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <view class="nav-tab-list">
  3. <view class="main">
  4. <view v-for="(item,index) in tabs" :key="index" @click="changeTab(index)" :style="getWstyle[index]" :class="selectIndex == index ? 'ft16 ftw600' : 'ft14 ftw500' " class="text-center ">
  5. {{item}}
  6. </view>
  7. </view>
  8. <view class="bd" :style="getL"></view>
  9. </view>
  10. </template>
  11. <script>
  12. export default{
  13. props:{
  14. isMain:{
  15. type:Boolean,
  16. default:true
  17. },
  18. tabs:{
  19. type:Array,
  20. default:function(){
  21. return new Array;
  22. }
  23. },
  24. selectIndex:{
  25. type:Number,
  26. default:0
  27. }
  28. },
  29. computed:{
  30. getW(){
  31. if(this.tabs.length == 0) return 0;
  32. let len = this.tabs.length;
  33. let w = 100/len;
  34. return w;
  35. },
  36. getWstyle(){
  37. let arr = new Array;
  38. for(var a in this.tabs){
  39. let style = 'width:'+this.getW + '%;';
  40. if(this.selectIndex == a){
  41. style+= 'color:'+ (this.isMain ? this.tempColor : '#5E40FF')+';';
  42. }else{
  43. style+='color:#333333;';
  44. }
  45. arr.push(style);
  46. }
  47. return arr;
  48. },
  49. getL(){
  50. let w = this.getW;
  51. let w2 = w/2;
  52. let l = this.selectIndex * w + w2;
  53. let style = 'left:calc('+l+'% - '+uni.upx2px(18)+'px);';
  54. if(this.isMain){
  55. style += 'background:'+this.tempColor+';';
  56. }
  57. return style;
  58. }
  59. },
  60. data(){
  61. return {
  62. }
  63. },
  64. methods:{
  65. changeTab(index){
  66. this.$emit('change',index);
  67. }
  68. }
  69. }
  70. </script>
  71. <style>
  72. .nav-tab-list{
  73. height:100rpx;
  74. position: relative;
  75. }
  76. .nav-tab-list .main{
  77. width: 100%;
  78. height: 100rpx;
  79. display: flex;
  80. align-items: center;
  81. }
  82. .nav-tab-list .bd{
  83. width: 36rpx;
  84. height:10rpx;
  85. background:#5E40FF;
  86. border-radius:6rpx 6rpx 0rpx 0rpx;
  87. position: absolute;
  88. left: 0;
  89. bottom: 0;
  90. z-index: 2;
  91. transition: left 0.4s;
  92. }
  93. </style>