当前位置:首页 > VUE

vue实现滑动tab

2026-02-17 22:42:18VUE

Vue 实现滑动 Tab 的方法

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

通过 Vue 的响应式数据和 CSS 过渡效果实现滑动 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 的示例:

<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:

vue实现滑动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中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…

vue实现数据

vue实现数据

Vue 实现数据绑定的方法 Vue.js 提供了多种方式实现数据绑定,包括双向绑定、单向绑定以及动态数据绑定。以下是几种常见的实现方式: 双向数据绑定(v-model) 双向数据绑定通常用于表单元素…

vue实现城市

vue实现城市

Vue 实现城市选择功能 使用 Element UI 的 Cascader 组件 Element UI 提供了一个 Cascader 级联选择器组件,非常适合实现城市选择功能。需要先安装 Elemen…

用vue实现滚动

用vue实现滚动

实现滚动的基本方法 在Vue中实现滚动可以通过多种方式完成,包括使用原生JavaScript、Vue指令或第三方库。以下是几种常见的方法: 使用ref和原生JavaScript 在模板中定义一个可滚…

vue实现切换隐藏

vue实现切换隐藏

使用 v-show 指令实现切换隐藏 在 Vue 中,v-show 是最简单的切换元素显示/隐藏的方法。它通过 CSS 的 display 属性控制元素可见性。 <template>…