当前位置:首页 > VUE

vue实现滑动tab

2026-02-17 22:42:18VUE

Vue 实现滑动 Tab 的方法

使用 Vue 和 CSS 实现基础滑动 Tab

通过 Vue 的响应式数据和 CSS 过渡效果实现滑动 Tab。以下是一个基础实现示例:

vue实现滑动tab

<template>
  <div class="tab-container">
    <div class="tab-header">
      <div 
        v-for="(tab, index) in tabs" 
        :key="index"
        class="tab-item"
        :class="{ 'active': activeTab === index }"
        @click="activeTab = index"
      >
        {{ tab.title }}
      </div>
      <div class="tab-indicator" :style="indicatorStyle"></div>
    </div>
    <div class="tab-content">
      <div 
        v-for="(tab, index) in tabs" 
        :key="index"
        v-show="activeTab === index"
      >
        {{ tab.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 0,
      tabs: [
        { title: 'Tab 1', content: 'Content 1' },
        { title: 'Tab 2', content: 'Content 2' },
        { title: 'Tab 3', content: 'Content 3' }
      ]
    }
  },
  computed: {
    indicatorStyle() {
      return {
        width: `${100 / this.tabs.length}%`,
        transform: `translateX(${this.activeTab * 100}%)`
      }
    }
  }
}
</script>

<style>
.tab-container {
  width: 100%;
}
.tab-header {
  display: flex;
  position: relative;
  border-bottom: 1px solid #ddd;
}
.tab-item {
  flex: 1;
  text-align: center;
  padding: 10px;
  cursor: pointer;
}
.tab-item.active {
  color: #42b983;
}
.tab-indicator {
  position: absolute;
  bottom: 0;
  height: 2px;
  background-color: #42b983;
  transition: transform 0.3s ease;
}
.tab-content {
  padding: 20px;
}
</style>

使用第三方库实现更复杂效果

如果需要更复杂的滑动效果,可以使用 vue-slickswiper-vue 等库。以下是使用 swiper-vue 的示例:

vue实现滑动tab

<template>
  <swiper
    :slides-per-view="3"
    :space-between="10"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-for="(tab, index) in tabs" :key="index">
      <div @click="activeTab = index" :class="{ 'active': activeTab === index }">
        {{ tab.title }}
      </div>
    </swiper-slide>
  </swiper>
  <div class="tab-content">
    {{ tabs[activeTab].content }}
  </div>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/swiper-bundle.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      activeTab: 0,
      tabs: [
        { title: 'Tab 1', content: 'Content 1' },
        { title: 'Tab 2', content: 'Content 2' },
        { title: 'Tab 3', content: 'Content 3' }
      ],
      swiper: null
    }
  },
  methods: {
    onSwiper(swiper) {
      this.swiper = swiper
    },
    onSlideChange() {
      this.activeTab = this.swiper.activeIndex
    }
  }
}
</script>

响应式设计考虑

对于移动端适配,可以添加媒体查询调整 Tab 的显示方式:

@media (max-width: 768px) {
  .tab-header {
    overflow-x: auto;
    white-space: nowrap;
    display: block;
  }
  .tab-item {
    display: inline-block;
    width: auto;
    padding: 10px 20px;
  }
  .tab-indicator {
    display: none;
  }
}

性能优化建议

对于大量 Tab 内容,可以使用 keep-alive 缓存已访问的 Tab:

<template>
  <div class="tab-content">
    <keep-alive>
      <component :is="tabs[activeTab].component" v-if="tabs[activeTab].component" />
    </keep-alive>
  </div>
</template>

以上方法提供了从基础到进阶的 Vue 滑动 Tab 实现方案,可根据项目需求选择合适的实现方式。

标签: vuetab
分享给朋友:

相关文章

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template>…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout() {…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一…