当前位置:首页 > 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 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…

vue实现树形

vue实现树形

Vue 实现树形组件 在 Vue 中实现树形结构通常需要递归组件和动态数据绑定。以下是实现树形组件的几种常见方法。 递归组件实现 递归组件是 Vue 中实现树形结构的核心方法。通过组件调用自身,可以…

vue 实现 confirm

vue 实现 confirm

实现确认对话框的方法 在Vue中实现确认对话框可以通过多种方式完成,包括使用内置组件、第三方库或自定义组件。 使用浏览器原生confirm 最简单的实现方式是直接调用浏览器原生的confirm方法。…