当前位置:首页 > VUE

vue实现滚动tab

2026-02-18 20:33:08VUE

实现滚动 Tab 的基本思路

滚动 Tab 通常用于横向导航栏,当内容超出容器宽度时支持左右滑动。核心逻辑是通过监听滚动事件或手动滑动操作,动态调整 Tab 的位置。

使用 CSS 实现基础滚动

通过 white-space: nowrapoverflow-x: auto 实现横向滚动:

<div class="scroll-tab-container">
  <div class="tab-list">
    <div class="tab-item" v-for="item in tabs" :key="item.id">{{ item.name }}</div>
  </div>
</div>
.scroll-tab-container {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch; /* 平滑滚动 */
}

.tab-list {
  display: flex;
  white-space: nowrap;
}

.tab-item {
  padding: 8px 16px;
  flex-shrink: 0;
}

动态计算滚动位置

通过 ref 获取 DOM 并计算滚动位置,实现主动滚动到指定 Tab:

vue实现滚动tab

<div class="scroll-tab-container" ref="container">
  <div class="tab-list" ref="tabList">
    <div class="tab-item" 
         v-for="item in tabs" 
         :key="item.id"
         @click="scrollToTab(item.id)"
         :ref="'tab_' + item.id">
      {{ item.name }}
    </div>
  </div>
</div>
methods: {
  scrollToTab(tabId) {
    const container = this.$refs.container;
    const tabElement = this.$refs[`tab_${tabId}`][0];
    const containerWidth = container.offsetWidth;
    const tabLeft = tabElement.offsetLeft;
    const tabWidth = tabElement.offsetWidth;

    container.scrollTo({
      left: tabLeft - (containerWidth - tabWidth) / 2,
      behavior: 'smooth'
    });
  }
}

添加滚动指示器

通过监听滚动事件,显示左右滚动箭头:

<button @click="scroll(-100)" :disabled="!canScrollLeft">←</button>
<div class="scroll-tab-container" @scroll="handleScroll">
  <!-- Tab 内容 -->
</div>
<button @click="scroll(100)" :disabled="!canScrollRight">→</button>
data() {
  return {
    canScrollLeft: false,
    canScrollRight: false
  }
},
methods: {
  handleScroll() {
    const container = this.$refs.container;
    this.canScrollLeft = container.scrollLeft > 0;
    this.canScrollRight = 
      container.scrollLeft + container.offsetWidth < container.scrollWidth;
  },
  scroll(offset) {
    this.$refs.container.scrollBy({
      left: offset,
      behavior: 'smooth'
    });
  }
}

使用第三方库

对于复杂需求,可考虑以下库:

vue实现滚动tab

  • vue-tabs:提供预置的滚动 Tab 功能
  • swiper/vue:实现触摸友好的滑动效果

安装 Swiper 示例:

npm install swiper
<swiper :slides-per-view="'auto'" :space-between="10">
  <swiper-slide v-for="item in tabs" :key="item.id">
    {{ item.name }}
  </swiper-slide>
</swiper>

响应式处理

根据窗口大小调整显示的 Tab 数量:

mounted() {
  window.addEventListener('resize', this.updateVisibleTabs);
},
beforeDestroy() {
  window.removeEventListener('resize', this.updateVisibleTabs);
},
methods: {
  updateVisibleTabs() {
    const containerWidth = this.$refs.container.offsetWidth;
    // 根据容器宽度计算可显示的 Tab 数量
  }
}

标签: vuetab
分享给朋友:

相关文章

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncComp…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &…

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…