当前位置:首页 > VUE

vue实现滑动tab

2026-03-09 11:21:01VUE

实现滑动 Tab 的基本思路

在 Vue 中实现滑动 Tab 可以通过结合 v-for 动态渲染 Tab 标签,使用 CSS 控制滑动效果,并通过 Vue 的响应式数据管理当前选中状态。核心在于动态计算滑动条的偏移位置和宽度。

vue实现滑动tab

基础代码结构

<template>
  <div class="sliding-tabs">
    <div class="tabs-header">
      <div 
        v-for="(tab, index) in tabs" 
        :key="index"
        class="tab-item"
        :class="{ 'active': activeIndex === index }"
        @click="switchTab(index)"
      >
        {{ tab.label }}
      </div>
      <div class="slider" :style="sliderStyle"></div>
    </div>
    <div class="tabs-content">
      <div v-for="(tab, index) in tabs" :key="index" v-show="activeIndex === index">
        {{ tab.content }}
      </div>
    </div>
  </div>
</template>

数据与样式定义

<script>
export default {
  data() {
    return {
      tabs: [
        { label: 'Tab 1', content: 'Content 1' },
        { label: 'Tab 2', content: 'Content 2' },
        { label: 'Tab 3', content: 'Content 3' }
      ],
      activeIndex: 0,
      sliderWidth: 0,
      sliderOffset: 0
    };
  },
  computed: {
    sliderStyle() {
      return {
        width: `${this.sliderWidth}px`,
        transform: `translateX(${this.sliderOffset}px)`
      };
    }
  },
  methods: {
    switchTab(index) {
      this.activeIndex = index;
      this.updateSliderPosition();
    },
    updateSliderPosition() {
      const tabElements = document.querySelectorAll('.tab-item');
      if (tabElements[this.activeIndex]) {
        this.sliderWidth = tabElements[this.activeIndex].offsetWidth;
        this.sliderOffset = tabElements[this.activeIndex].offsetLeft;
      }
    }
  },
  mounted() {
    this.updateSliderPosition();
    window.addEventListener('resize', this.updateSliderPosition);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.updateSliderPosition);
  }
};
</script>

CSS 样式

<style scoped>
.sliding-tabs {
  width: 100%;
}

.tabs-header {
  display: flex;
  position: relative;
  border-bottom: 1px solid #eee;
}

.tab-item {
  padding: 10px 20px;
  cursor: pointer;
  transition: color 0.3s;
}

.tab-item.active {
  color: #42b983;
}

.slider {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 2px;
  background-color: #42b983;
  transition: all 0.3s ease;
}

.tabs-content {
  padding: 20px 0;
}
</style>

动态滑动实现说明

  1. 滑动条计算:通过 offsetWidthoffsetLeft 动态获取当前选中 Tab 的宽度和偏移位置,赋值给 sliderWidthsliderOffset
  2. 响应式更新:监听窗口 resize 事件,确保滑动条位置随窗口大小变化重新计算。
  3. 过渡动画:CSS 的 transition 属性为滑动条添加平滑移动效果。

扩展功能

  • 动态 Tab 数据:通过接口异步加载 tabs 数据,需在数据更新后调用 this.$nextTick(() => this.updateSliderPosition())
  • 滚动支持:若 Tab 数量过多,可为 tabs-header 添加横向滚动条(overflow-x: auto),并动态调整滑动条位置。

注意事项

  • 确保 Tab 项的宽度一致或动态计算,避免滑动条宽度跳动。
  • 组件销毁时移除事件监听,避免内存泄漏。

标签: vuetab
分享给朋友:

相关文章

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="bu…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…