当前位置:首页 > VUE

vue实现选项卡分页

2026-03-27 04:17:08VUE

使用 Vue 实现选项卡分页

基础实现

通过 v-for 动态生成选项卡标题,利用 v-showv-if 控制内容显示。

<template>
  <div class="tabs">
    <div class="tab-headers">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="currentTab = index"
        :class="{ active: currentTab === index }"
      >
        {{ tab.title }}
      </button>
    </div>
    <div class="tab-contents">
      <div 
        v-for="(tab, index) in tabs" 
        :key="index"
        v-show="currentTab === index"
      >
        {{ tab.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 0,
      tabs: [
        { title: 'Tab 1', content: 'Content 1' },
        { title: 'Tab 2', content: 'Content 2' },
        { title: 'Tab 3', content: 'Content 3' }
      ]
    }
  }
}
</script>

<style>
.tab-headers button.active {
  background-color: #42b983;
  color: white;
}
</style>

动态内容加载

结合异步组件或 API 请求实现按需加载内容。

methods: {
  async loadContent(index) {
    if (!this.tabs[index].loaded) {
      const res = await fetch(`/api/content/${index}`);
      this.tabs[index].content = await res.json();
      this.tabs[index].loaded = true;
    }
    this.currentTab = index;
  }
}

过渡动画效果

使用 Vue 的 <transition> 组件实现切换动画。

<transition name="fade" mode="out-in">
  <div 
    v-for="(tab, index) in tabs"
    v-show="currentTab === index"
    :key="index"
  >
    {{ tab.content }}
  </div>
</transition>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

可复用组件封装

将选项卡逻辑抽象为独立组件。

<!-- TabContainer.vue -->
<template>
  <div class="tab-container">
    <slot name="headers"></slot>
    <slot name="contents"></slot>
  </div>
</template>

<!-- 使用方式 -->
<tab-container>
  <template v-slot:headers>
    <button v-for="(tab, index) in tabs" @click="currentTab = index">
      {{ tab.title }}
    </button>
  </template>
  <template v-slot:contents>
    <div v-for="(tab, index) in tabs" v-show="currentTab === index">
      {{ tab.content }}
    </div>
  </template>
</tab-container>

响应式设计

通过 CSS 媒体查询实现移动端适配。

vue实现选项卡分页

@media (max-width: 768px) {
  .tab-headers {
    flex-direction: column;
  }
}

标签: 分页选项卡
分享给朋友:

相关文章

vue组件传值实现分页

vue组件传值实现分页

Vue 组件传值实现分页 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方法: 使用props和$emit 父组件通过props向子组件传递数据,子组件通过$emit向…

vue组件传值实现分页

vue组件传值实现分页

Vue组件传值实现分页的方法 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息:…

vue实现下拉分页思想

vue实现下拉分页思想

实现下拉分页的基本思想 下拉分页(Infinite Scroll)是一种常见的分页加载方式,当用户滚动到页面底部时自动加载下一页数据。Vue 中可以通过监听滚动事件或使用第三方库实现。 监听滚动事件…

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以直…

h5实现左右分页

h5实现左右分页

实现左右分页的HTML5方法 在HTML5中实现左右分页效果可以通过多种方式完成,以下是几种常见的方法: 使用CSS Flexbox布局 Flexbox是一种现代的布局方式,可以轻松实现左右分页效果…

vue实现页面分页

vue实现页面分页

Vue实现页面分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,适合快速集成。以下是一个示例代码: <template>…