当前位置:首页 > 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列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue实现城市

vue实现城市

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