navTab.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view class="navTabBox">
  3. <view class="longTab">
  4. <scroll-view scroll-x="true" style="white-space: nowrap; display: flex" scroll-with-animation :scroll-left="isLeft-changeLeft">
  5. <view
  6. class="longItem"
  7. :style="'min-width:' + isWidth + 'px'"
  8. :data-index="index"
  9. :class="index === tabClick ? 'click' : ''"
  10. v-for="(item, index) in tabTitle"
  11. :key="index"
  12. :id="'id' + index"
  13. @click="longClick(index)"
  14. >
  15. {{ item }}
  16. </view>
  17. <view class="underlineBox" :style="'transform:translateX(' + isLeft + 'px);width:' + lineWidth + 'px'"><view class="underline"></view></view>
  18. </scroll-view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'navTab',
  25. props: {
  26. tabTitle: {
  27. type: Array
  28. }
  29. },
  30. data() {
  31. return {
  32. tabClick: 0, //导航栏被点击
  33. isLeft: 0, //导航栏下划线位置
  34. isWidth: 0, //每个导航栏占位
  35. tabLeft: 0,
  36. leftList:[],
  37. lineWidth:0,
  38. wWidth:0
  39. };
  40. },
  41. computed: {
  42. changeLeft() {
  43. return this.wWidth/2 - this.leftList[this.tabClick]/2
  44. }
  45. },
  46. created() {
  47. var that = this;
  48. // 获取设备宽度
  49. uni.getSystemInfo({
  50. success(e) {
  51. that.wWidth = e.windowWidth;
  52. if (that.tabTitle.length <= 5) {
  53. that.isWidth = e.windowWidth / that.tabTitle.length; //宽度除以导航标题个数=一个导航所占宽度
  54. } else {
  55. that.isWidth = e.windowWidth / 5;
  56. }
  57. }
  58. });
  59. console.log('load')
  60. },
  61. onReady() {
  62. console.log('ready')
  63. const query = uni.createSelectorQuery().in(this);
  64. query.selectAll('.longItem').boundingClientRect(data => {
  65. console.log(data)
  66. this.leftList = data.map((item,index)=>{
  67. return item.width
  68. })
  69. this.lineWidth = this.leftList[0]
  70. }).exec();
  71. console.log(this.leftList)
  72. },
  73. methods: {
  74. // 导航栏点击
  75. longClick(index) {
  76. this.tabClick = index; //设置导航点击了哪一个
  77. // this.isLeft = index * this.isWidth; //设置下划线位置
  78. this.isLeft = this.leftList.reduce((acc,cur,curIndex)=>{
  79. if(curIndex <index){
  80. return acc+cur
  81. }else{
  82. return acc
  83. }
  84. },0); //设置下划线位置
  85. this.lineWidth = this.leftList[index]
  86. console.log(this.isLeft)
  87. this.$emit('changeTab', index); //设置swiper的第几页
  88. // this.$parent.currentTab = index //设置swiper的第几页
  89. }
  90. }
  91. };
  92. </script>
  93. <style lang="scss">
  94. .navTabBox {
  95. width: 100vw;
  96. color: rgba(255, 255, 255, 0.5);
  97. .click {
  98. color: white;
  99. }
  100. .longTab {
  101. width: 100%;
  102. .longItem {
  103. height: 90upx;
  104. display: inline-block;
  105. line-height: 90upx;
  106. text-align: center;
  107. }
  108. .underlineBox {
  109. height: 3px;
  110. width: 20%;
  111. display: flex;
  112. align-content: center;
  113. justify-content: center;
  114. transition: 0.5s;
  115. .underline {
  116. width: 84upx;
  117. height: 4px;
  118. background-color: white;
  119. }
  120. }
  121. }
  122. .shortTab {
  123. width: 100%;
  124. .navTab {
  125. display: flex;
  126. width: 100%;
  127. height: 90upx;
  128. position: relative;
  129. .navTabItem {
  130. display: flex;
  131. align-items: center;
  132. justify-content: center;
  133. width: 100%;
  134. font-size: 28upx;
  135. }
  136. }
  137. .underlineBox {
  138. height: 3px;
  139. display: flex;
  140. align-content: center;
  141. justify-content: center;
  142. transition: 0.5s;
  143. .underline {
  144. width: 84upx;
  145. height: 3px;
  146. background-color: white;
  147. }
  148. }
  149. }
  150. }
  151. </style>