当前位置:首页 > VUE

vue实现滑动tab

2026-03-09 11:21:01VUE

vue实现滑动tab

vue实现滑动tab

实现滑动 Tab 的基本思路

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

基础代码结构

<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 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…

vue 实现工具

vue 实现工具

Vue 实现工具的方法 使用 Vue 实现工具功能可以通过多种方式完成,具体取决于工具的类型和需求。以下是几种常见的方法: 组件化实现 创建一个独立的 Vue 组件来封装工具功能。例如,实现一个计…