Vue3实现带动画效果的tab栏切换

效果图如下所示:

实现思路:

其实很简单

1、首先切换tab栏时tab标签激活下标与对应显示内容下标要一致。

2、其次点击tab栏切换时更新下标

3、最后就是css添加动画效果

这样就了!!!

上全部代码

<template>
  <div class="container">
    <el-card>
      <el-button type="text">1、tab栏切换(针对格式相同的内容)<i class="el-icon-s-order" /></el-button>
      <div class="tab-nav">
      <ul class="tab-tilte">
        <li v-for="(item, index) in tabs" :key="index" :class="{active:tabIndex==index}" @click="changeTab(index)">{
  { item }}</li>
      </ul>
    </div>
      <div class="tab-content" :style="{ transform: `translateX(${-tabIndex * 100}%)` }">
        <div v-for="(item, index) in contents" :key="index" class="tab-item">{
  { item.name }}</div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
    const tabIndex = ref(0);
    const tabs = ['tab栏1', 'tab栏2', 'tab栏3', 'tab栏4'];
    const contents=[
      {
        id:'1',
        name:'内容1',
        pic:'',
        title:'tab栏一区域'
      },
      {
        id:'2',
        name:'内容2',
        pic:'',
        title:'tab栏二区域'
      },
      {
        id:'3',
        name:'内容3',
        pic:'',
        title:'tab栏三区域'
      },
      {
        id:'4',
        name:'内容4',
        pic:'',
        title:'tab栏四区域'
      }
    ];
    const changeTab = (index) => {
      tabIndex.value = index;
    };
 
</script>

<style lang="scss" scoped>
.container {
  width: 96%;
  margin: 2%;
}
.tab-nav ul{//ul默认有40左边距
  padding-left: 0px !important;
}
ul li {
  margin: 0;
  padding: 0;
  list-style: none;
}

.tab-tilte {
  display: flex;
}

.tab-tilte li {
  flex: 1;
  padding: 10px;
  text-align: center;
  background-color: #f4f4f4;
  cursor: pointer;
  transition: background-color 0.3s; /* 添加过渡效果 */
}

/* 点击对应的标题添加对应的背景颜色 */
.tab-tilte .active {
  background-color: #09f;
  color: #fff;
}

.tab-content {
  display: flex;
  transition: transform 0.5s ease; /* 添加过渡效果,并使用缓动函数 */
}

.tab-item {
  flex: 1;
  min-width: 96%;
  margin: 2%;
  line-height: 100px;
  text-align: left;
  background: rgb(0, 255, 200);
}
</style>

相关推荐

  1. js实现tab切换

    2024-02-22 00:16:02       34 阅读
  2. Vue实现table左右切换平滑过渡效果

    2024-02-22 00:16:02       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-22 00:16:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-22 00:16:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-22 00:16:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-22 00:16:02       20 阅读

热门阅读

  1. 工具栏和菜单栏的关系是什么?

    2024-02-22 00:16:02       41 阅读
  2. 表空间查询sql

    2024-02-22 00:16:02       27 阅读
  3. 工作记录之策略模式

    2024-02-22 00:16:02       27 阅读
  4. 代码随想录三刷day04

    2024-02-22 00:16:02       30 阅读
  5. kali kvm

    2024-02-22 00:16:02       33 阅读
  6. QT3作业

    QT3作业

    2024-02-22 00:16:02      25 阅读
  7. 通过Redis增减库存避坑

    2024-02-22 00:16:02       32 阅读
  8. C#_值类型与引用类型 及 值参数与引用参数

    2024-02-22 00:16:02       25 阅读
  9. 开源软件的影响力

    2024-02-22 00:16:02       26 阅读