当前位置:首页 > 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 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…

vue实现反转

vue实现反转

实现数组反转 在Vue中反转数组可以通过多种方式实现,以下是几种常见方法: 使用JavaScript原生reverse方法 // 在methods中定义方法 methods: { revers…

react实现vue

react实现vue

React 实现 Vue 功能 React 和 Vue 是两种不同的前端框架,但可以通过一些方法在 React 中实现 Vue 的特性。以下是几种常见 Vue 功能在 React 中的实现方式: 双…

vue 实现获取焦点

vue 实现获取焦点

实现获取焦点的方法 在 Vue 中实现获取焦点可以通过多种方式完成,以下是几种常见的方法: 使用 ref 和 focus 方法 通过 ref 属性获取 DOM 元素,并调用其 focus 方法实现焦…

vue的实现机制

vue的实现机制

Vue 的实现机制 Vue 是一个渐进式 JavaScript 框架,其核心实现机制主要包括响应式系统、虚拟 DOM、模板编译和组件化设计。以下是 Vue 实现机制的主要组成部分: 响应式系统 Vu…