• vue3+ts实现Tab滚动居中


    <template>
      <div class="tab-container">
        <div class="tab-list" ref="tabList">
          <div
            v-for="(tab, index) in tabs"
            :key="index"
            :class="['tab', { active: activeTab === index }]"
            @click="changeTab(index)"
          >
            {{ tab }}
          div>
        div>
      div>
    template>
    
    <script lang="ts">
    import { ref, onMounted } from 'vue';
    
    export default {
      name: 'TabContainer',
      data() {
        return {
          tabs: ['Tab 1', 'Tab 2', 'Tab 3', 'Tab 4', 'Tab 5'],
          activeTab: 0,
        };
      },
      mounted() {
        onMounted(() => {
          this.centerActiveTab();
        });
      },
      methods: {
        changeTab(index: number) {
          this.activeTab = index;
          this.centerActiveTab();
        },
        centerActiveTab() {
          const tabList = this.$refs.tabList as HTMLElement;
          const activeTab = tabList.querySelector('.active') as HTMLElement;
    
          if (activeTab) {
            const containerWidth = tabList.offsetWidth;
            const activeTabWidth = activeTab.offsetWidth;
            const activeTabLeft = activeTab.offsetLeft;
    
            const scrollLeft = activeTabLeft - (containerWidth - activeTabWidth) / 2;
            tabList.scrollTo({
              left: scrollLeft,
              behavior: 'smooth',
            });
          }
        },
      },
    };
    script>
    
    <style scoped>
    .tab-container {
      width: 100%;
      overflow-x: auto;
    }
    
    .tab-list {
      display: flex;
    }
    
    .tab {
      padding: 10px;
      cursor: pointer;
    }
    
    .active {
      font-weight: bold;
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    TabContainer组件中使用了ref来获取到tabList元素的引用,然后在mounted钩子函数中调用centerActiveTab方法来初始化时居中显示当前激活的Tab。

    changeTab方法中,切换Tab时,会更新activeTab的值,并调用centerActiveTab方法来将新的激活Tab居中显示。

    centerActiveTab方法中,首先获取到tabList和当前激活的Tab元素的引用,然后计算出滚动的位置,使激活的Tab居中显示。通过调用scrollTo方法来实现滚动效果

    最后,在样式中设置了容器的宽度为100%,并设置了overflow-x: auto来实现水平滚动的效果。每个Tab元素设置了一些基本的样式,激活的Tab元素设置了加粗的字体。

  • 相关阅读:
    C语言编写简易图书管理系统
    20k招的工程师刚加入团队,4行代码写3个NPE异常,服了!
    bpftrace:简便输出调试信息
    AWTK实现汽车仪表Cluster/DashBoard嵌入式GUI开发(七):FreeRTOS移植
    云计算发展
    MongoDB副本集&集群原理
    【已解决】SpringBoot图片更新需重启服务器才能显示
    Django--31Django知识体系梳理总结
    JavaScript一些单行代码技巧
    Linux下uboot添加自定义命令(详细)实例及原理解析
  • 原文地址:https://blog.csdn.net/qq_45914628/article/details/136380898