当前位置:首页 > 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;
  }
}

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

相关文章

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…

vue page 实现分页

vue page 实现分页

实现 Vue 分页功能的方法 使用 Element UI 的分页组件 安装 Element UI 后,可以直接使用 el-pagination 组件实现分页功能。 <template>…

vue实现分页按钮

vue实现分页按钮

实现分页按钮的基本思路 在Vue中实现分页按钮通常需要结合计算属性、事件绑定和动态样式。核心逻辑包括计算总页数、生成页码数组、处理页码切换事件以及高亮当前页码。 基础实现代码示例 <templ…

vue完整实现分页

vue完整实现分页

Vue 分页组件实现 分页组件模板 分页组件通常包含页码按钮、上一页/下一页按钮和跳转功能。以下是一个基础模板结构: <template> <div class="pagina…

vue 异步分页实现

vue 异步分页实现

异步分页实现方法 在Vue中实现异步分页通常涉及与后端API的交互,前端需要处理分页逻辑和数据加载。以下是常见的实现方式: 使用axios获取分页数据 通过axios与后端API通信,获取分页数据并…

vue实现分页6

vue实现分页6

Vue实现分页的6种方法 基础分页组件实现 使用Vue的v-for指令和计算属性实现基础分页功能。通过计算总页数并动态渲染页码按钮,结合v-model绑定当前页码。 <template>…