navTab.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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="tabLeft">
  5. <view class="longItem" :style='"width:"+isWidth+"px"' :data-index="index" :class="index===tabClick?'click':''" v-for="(item,index) in tabTitle" :key="index" :id="'id'+index" @click="longClick(index)">{{item}}</view>
  6. <view class="underlineBox" :style='"transform:translateX("+isLeft+"px);width:"+isWidth+"px"'>
  7. <view class="underline"></view>
  8. </view>
  9. </scroll-view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'navTab',
  16. props: {
  17. tabTitle: {
  18. type: Array,
  19. default: []
  20. }
  21. },
  22. data() {
  23. return {
  24. tabClick: 0, //导航栏被点击
  25. isLeft: 0, //导航栏下划线位置
  26. isWidth: 0, //每个导航栏占位
  27. tabLeft:0
  28. };
  29. },
  30. created() {
  31. var that = this
  32. // 获取设备宽度
  33. uni.getSystemInfo({
  34. success(e) {
  35. if(that.tabTitle.length<= 5 ){
  36. that.isWidth = e.windowWidth / that.tabTitle.length //宽度除以导航标题个数=一个导航所占宽度
  37. } else {
  38. that.isWidth = e.windowWidth / 5
  39. }
  40. }
  41. })
  42. },
  43. methods: {
  44. // 导航栏点击
  45. longClick(index){
  46. if(this.tabTitle.length>5){
  47. var tempIndex = index - 2;
  48. tempIndex = tempIndex<=0 ? 0 : tempIndex;
  49. this.tabLeft = (index-2) * this.isWidth //设置下划线位置
  50. }
  51. this.tabClick = index //设置导航点击了哪一个
  52. this.isLeft = index * this.isWidth //设置下划线位置
  53. this.$emit('changeTab', index);//设置swiper的第几页
  54. // this.$parent.currentTab = index //设置swiper的第几页
  55. }
  56. }
  57. }
  58. </script>
  59. <style lang="scss">
  60. .navTabBox {
  61. width: 100vw;
  62. color: rgba(255, 255, 255, 0.50);
  63. .click {
  64. color: white;
  65. }
  66. .longTab {
  67. width: 100%;
  68. .longItem{
  69. height: 90upx;
  70. display: inline-block;
  71. line-height: 90upx;
  72. text-align: center;
  73. }
  74. .underlineBox {
  75. height: 3px;
  76. width: 20%;
  77. display: flex;
  78. align-content: center;
  79. justify-content: center;
  80. transition: .5s;
  81. .underline {
  82. width: 84upx;
  83. height: 4px;
  84. background-color: white;
  85. }
  86. }
  87. }
  88. }
  89. </style>