tabvsw.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. }
  80. .tab-nav-plus-main{
  81. width: 100%;
  82. height: 80rpx;
  83. padding: 20rpx 0rpx 0rpx 0rpx;
  84. /* background: #FFFFFF; */
  85. /* border-top: 0rpx solid #E4E6ED; */
  86. }
  87. .tab-nav-plus-main.fixed{
  88. //position: fixed;
  89. left: 0;
  90. z-index: 20;
  91. background: #f8f8f8;
  92. box-shadow:0rpx 4rpx 16rpx 0rpx rgba(0,0,0,0.08);
  93. }
  94. .tab-nav-plus-scroll{
  95. width: 100%;
  96. white-space: nowrap;
  97. height: 76rpx;
  98. }
  99. .tab-nav-plus-scroll .item{
  100. height: 60upx;
  101. line-height: 60upx;
  102. display: inline-block;
  103. margin-right: 20rpx;
  104. position: relative;
  105. }
  106. .tab-nav-plus-scroll .item:last-child{
  107. margin-right: 0;
  108. }
  109. .tab-nav-plus-scroll .item .tit{
  110. text-align: center;
  111. height: 48rpx;
  112. line-height: 44rpx;
  113. font-size: 30rpx;
  114. /* border: solid 1upx #ddd; */
  115. background: #f0f0f0;
  116. padding: 0upx 16upx;
  117. border-radius: 12upx;
  118. font-weight: 500;
  119. color: #909398;
  120. }
  121. .tab-nav-plus-scroll .item .tit.on{
  122. font-size: 28rpx;
  123. font-weight: 600;
  124. }
  125. .tab-nav-plus-main .tab-nav-plus-scroll .item .bd{
  126. position: absolute;
  127. z-index: 1;
  128. left: calc(50% - 18rpx);
  129. bottom: 0;
  130. width: 36rpx;
  131. height: 12rpx;
  132. border-radius: 6rpx 6rpx 0rpx 0rpx;
  133. }
  134. .tab-nav-plus-main.fixed .tab-nav-plus-scroll .item .bd{
  135. background: #FFFFFF;
  136. }
  137. </style>