Vue封装Tabs标签页

示例一 

组件Tabs:

<template>
  <div class="my-tabs">
    <div class="tabs-bar">
      <div class="tabs-bar-nav">
        <div
          class="tabs-tab"
          v-for="(tab, index) in tabList"
          :class="[tabIndex == tab.index ? 'tabs-active' : '']"
          :style="tab.style"
          @click="changeTab(tab)"
          :key="index"
        >
          {{ tab.name }}
        </div>
      </div>
    </div>
    <div class="tabs-content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyTabs",
  props: {
    tabList: Array,
    tabIndex: Number,
  },
  data() {
    return {};
  },
  methods: {
    changeTab: function (tab) {
      this.$emit("changeTab", tab);
    },
  },
};
</script>

<style lang="less" scoped>
.my-tabs {
  font-size: 14px;
  color: #444;
}
.tabs-bar {
  border-bottom: 1px solid #eee;
  position: relative;
  padding: 5px 0;
}
.tabs-bar-nav {
  display: table;
  position: absolute;
  bottom: -1px;
  width: 100%;
}
.tabs-tab {
  min-width: 110px;
  padding: 5px 0;
  position: relative;
  display: inline-block;
  text-align: center;
  cursor: pointer;
}
.tabs-active {
  border-bottom: 1px solid #409eff;
  color: #409eff;
}
.tabs-content {
  padding-left: 20px;
  padding-right: 20px;
  padding-bottom: 30px;
}
</style>

 引用组建Tabs:

<template>
  <div>
    <MyTabs :tabList="tabList" :tabIndex="tabIndex" @changeTab="changeTab">
      <div
        v-for="(item, index) in tabList"
        :key="index"
        :class="currentTab === item.index ? 'show' : 'hidden'"
      >
        {{ item.component }}
      </div>
    </MyTabs>
  </div>
</template>

<script>
import MyTabs from "../components/Tabs";

export default {
  name: "Tabs",
  data() {
    return {
      currentTab: 0,
      tabIndex: 0,
      currentContent: "one",
      tabList: [
        {
          index: 0,
          name: "选项一",
          component: "one",
          style: "width: 48%;",
        },
        {
          index: 1,
          name: "选项二",
          component: "two",
          style: "width: 48%;",
        },
      ],
    };
  },
  components: {
    MyTabs,
  },
  created() {},
  mounted() {},
  methods: {
    // 切换选项卡
    changeTab: function (tab) {
      this.tabIndex = tab.index;
      this.currentTab = tab.index;
    },
  },
};
</script>

<style lang="less" scoped>
.show {
  display: block;
}
.hidden {
  display: none;
}
</style>

 示例二

组件tabs:

<template>
  <div class="header" ref="hhh">
    <div class="header_search-left">
      <!-- <slot name="header_tab_img"></slot> -->
    </div>
    <div class="header_tab" ref="headertab">
      <ul ref="tabitem">
        <li
          v-for="(item, index) in listArray"
          :key="index"
          :class="index == current ? 'activeheader' : ''"
          @click="changeTab(index, $event, item)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <div class="header_search-right"></div>
  </div>
</template>

<script>
export default {
  name: "HeaderTab",
  props: {
    current: Number,
    listArray: {
      type: Array,
    },
  },
  data() {
    return {};
  },
  methods: {
    changeTab(index, e, item) {
      this.$emit("changeTab", item);
      this.current = index; // 高亮当前
      let tab = this.$refs.headertab; // 包裹 ul的 div
      let tabitem = this.$refs.tabitem; // 包裹 li的 ul
      let winWidth = this.$refs.hhh.clientWidth; // 当前屏幕的宽度
      let liList = e.target; // 当前点击的li
      if (liList) {
        //  当前li左偏移, li的宽度, 中间值(当前屏幕的宽度 - li的宽度) /2, 目标值 (中间值 - 当前li左偏移), 整个ul的宽度
        let liLeft = liList.offsetLeft,
          liWidth = liList.offsetWidth,
          liCenter = (winWidth - liWidth) / 2,
          liTarget = liLeft - liCenter;
        let ulWidth = tabitem.offsetWidth;
        if (liTarget < 0) {
          tab.scrollLeft = 0;
          return;
        }
        // winWidth(375) - ulWidth(436) =  -61
        if (liTarget < winWidth - ulWidth) {
          tab.scrollLeft = -(winWidth - ulWidth) + liWidth;
          return;
        }
        tab.scrollLeft = liTarget;
      }
    },
  },
};
</script>
<style lang="less" scoped>
.header {
  width: 100%;
  height: 40px;
  background-color: #fff;
  display: flex;
  border-radius: 8px;
  position: relative;
  overflow: hidden;
}
.header_tab {
  width: 90%;
  height: 40px;
  display: flex;
  flex-wrap: nowrap;
  overflow: scroll;
  padding-left: 19px;
  padding-right: 19px;
  // 平滑滚动
  transition: all 0.05s linear;
  scroll-behavior: smooth;
  scrollbar-width: none;
}
.header_tab::-webkit-scrollbar {
  display: none;
}
ul {
  display: inline-block;
  white-space: nowrap;
  margin: 0;
  padding-left: 0;
  padding-right: 19px;
  height: 40px;
}
li {
  display: inline-block;
  height: 40px;
  line-height: 40px;
  padding: 0px 10px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  // 点击高亮某一项时,将原来的字体变大,会导致没有高亮的元素距离底部有空隙,会出现纵向滚动条
  // margin-top: -1px;
}
.activeheader {
  font-size: 14px;
  color: #2b96ff;
  // font-weight: 700;
  position: relative;
  background: linear-gradient(
    180deg,
    rgba(71, 151, 220, 0) 0%,
    rgba(71, 151, 220, 0.35) 100%
  );
  &:after {
    position: absolute;
    content: "";
    width: 100%;
    height: 2px;
    /* bottom: 0; */
    left: calc(50% - 52px);
    background-color: #2b96ff;
    border-radius: 1px;
    height: 0.1rem;
    box-sizing: border-box;
    background-color: #3e75fc;
    position: absolute;
    left: 0;
    bottom: 0.05rem;
    z-index: 1;
    transition: transform 0.3s ease-in-out;
    transform-origin: 0 0;
  }
}
.header_search-left {
  width: 25px;
  height: 40px;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1000;
  background-image: -webkit-linear-gradient(
    to right,
    rgba(255, 255, 255, 1) 0%,
    rgba(255, 255, 255, 1) 55%,
    rgba(255, 255, 255, 1) 70%,
    rgba(255, 255, 255, 0.8) 80%,
    rgba(255, 255, 255, 0.3) 90%,
    rgba(255, 255, 255, 0.1) 100%
  );
  background-image: linear-gradient(
    to right,
    rgba(255, 255, 255, 1) 0%,
    rgba(255, 255, 255, 1) 55%,
    rgba(255, 255, 255, 1) 70%,
    rgba(255, 255, 255, 0.8) 80%,
    rgba(255, 255, 255, 0.3) 90%,
    rgba(255, 255, 255, 0.1) 100%
  );
  img {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
  }
}
.header_search-right {
  width: 25px;
  height: 40px;
  position: absolute;
  top: 0;
  right: 0;
  z-index: 1000;
  background-image: -webkit-linear-gradient(
    to left,
    rgba(255, 255, 255, 1) 0%,
    rgba(255, 255, 255, 1) 55%,
    rgba(255, 255, 255, 1) 70%,
    rgba(255, 255, 255, 0.8) 80%,
    rgba(255, 255, 255, 0.3) 90%,
    rgba(255, 255, 255, 0.1) 100%
  );
  background-image: linear-gradient(
    to left,
    rgba(255, 255, 255, 1) 0%,
    rgba(255, 255, 255, 1) 55%,
    rgba(255, 255, 255, 1) 70%,
    rgba(255, 255, 255, 0.8) 80%,
    rgba(255, 255, 255, 0.3) 90%,
    rgba(255, 255, 255, 0.1) 100%
  );
  img {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
  }
}
</style>

 引用组件tabs:

<template>
  <div style="width: 400px">
    <header-tab :listArray="list" :current="current" @changeTab="changeTab">
    </header-tab>
    <div
      v-for="(item, index) in list"
      :key="index"
      :class="currentTab === item.id ? 'show' : 'hidden'"
    >
      {{ item.name }}
    </div>
  </div>
</template>

<script>
import HeaderTab from "../components/tabs";

export default {
  components: {
    HeaderTab,
  },
  data() {
    return {
      current: 0,
      currentTab: 0,
      list: [
        { id: 0, name: "测试1" },
        { id: 1, name: "测试2" },
        { id: 2, name: "测试3" },
        { id: 3, name: "测试4" },
        { id: 4, name: "测试5" },
        { id: 5, name: "测试6" },
        { id: 6, name: "测试7" },
        { id: 7, name: "测试8" },
        { id: 8, name: "测试9" },
        { id: 9, name: "测试10" },
        { id: 10, name: "测试11" },
        { id: 11, name: "测试12" },
        { id: 12, name: "测试13" },
        { id: 13, name: "测试14" },
        { id: 14, name: "测试15" },
        { id: 15, name: "测试16" },
        { id: 16, name: "测试17" },
        { id: 17, name: "测试18" },
        { id: 18, name: "测试19" },
      ],
    };
  },
  created() {},
  mounted() {},
  methods: {
    // 切换选项卡
    changeTab(tab) {
      this.current = tab.id;
      this.currentTab = tab.id;
    },
  },
};
</script>

<style lang="less" scoped>
.show {
  display: block;
}
.hidden {
  display: none;
}
</style>

<think>好的,我需要帮助用户找到在Vue中使用Element Plus自定义Tabs标签页的方法或示例。首先,用户已经提供了三个引用内容,我需要仔细分析这些引用中的信息,看看是否有相关内容。 引用[1]提到Element UI和Element Plus中的el-tabs组件,说明两者的基本用法相似。这里可能包含一些基础的使用方法,但用户需要的是自定义,所以可能需要进一步的信息。 引用[2]具体讨论了如何去掉Element Plus中Tabs底部的灰色线条,这属于样式自定义的一部分。这说明可以通过修改CSS来实现某些视觉上的调整,这可能对用户有帮助。 引用[3]提到了自定义Tabs的原因、组件创建和使用示例,但用户提供的引用内容似乎不完整,可能没有详细的步骤。不过,结合这个引用,可以推测可能需要创建自定义组件并覆盖默认样式或行为。 接下来,我需要综合这些信息,构建一个回答。首先,用户可能需要了解如何修改Element Plus的Tabs组件样式,比如隐藏下划线,这可能涉及CSS覆盖。其次,可能涉及到自定义内容,比如添加图标或按钮,这需要插槽的使用。另外,引用[3]提到的创建自定义组件可能涉及到更高级的定制,比如扩展el-tabs的功能。 需要确保回答结构清晰,分步骤说明。可能包括以下部分: 1. 基础使用,确保用户了解基本用法。 2. 样式自定义,如隐藏底部线条,调整激活样式。 3. 内容自定义,使用插槽添加额外元素。 4. 高级自定义,比如创建自定义Tabs组件。 需要检查每个步骤是否符合Element Plus的文档,并参考提供的引用。例如,引用[2]中的方法是通过CSS隐藏下划线,这需要添加到全局或局部样式表中。同时,引用[3]可能涉及组件封装,但具体实现可能需要进一步说明。 最后,生成相关问题,引导用户进一步探索,比如性能优化、动态生成标签等。</think>[用户可见层] # Vue Element Plus 自定义 Tabs 标签页实现教程 ## 一、基础用法 Element Plus 的 `el-tabs` 组件默认提供基础布局与交互,使用前需先安装 Element Plus: ```bash npm install element-plus ``` 基础代码示例: ```vue <template> <el-tabs v-model="activeTab"> <el-tab-pane label="首页" name="home">内容区域1</el-tab-pane> <el-tab-pane label="产品" name="product">内容区域2</el-tab-pane> </el-tabs> </template> ``` ## 二、样式自定义 ### 1. 隐藏底部线条 在全局或组件样式文件中添加 CSS: ```css .el-tabs__nav-wrap::after { background-color: transparent !important; } ``` 此方法通过覆盖默认样式移除灰色下划线[^2] ### 2. 修改激活状态样式 ```css .el-tabs__item.is-active { color: #ff6b6b; border-bottom: 2px solid #ff6b6b; } ``` ## 三、内容扩展 ### 1. 添加图标与按钮 利用插槽实现复合内容: ```vue <el-tabs> <template #default> <el-tab-pane label="消息中心"> <template #label> <el-icon><message /></el-icon> 消息 (99+) </template> 消息列表... </el-tab-pane> </template> <template #prefix> <el-button @click="handleAddTab">+</el-button> </template> </el-tabs> ``` ### 2. 动态标签页 ```vue <script setup> const tabs = ref([ { name: 'tab1', label: 'Tab 1' } ]); const addTab = () => { tabs.value.push({ name: Date.now(), label: `New Tab` }); } </script> ``` ## 四、高级封装 创建可复用的增强型组件: ```vue <!-- custom-tabs.vue --> <template> <el-tabs v-bind="$attrs" :class="{'no-border': hideBorder}" @tab-click="handleTabChange" > <slot /> </el-tabs> </template> <script setup> defineProps({ hideBorder: Boolean }) const handleTabChange = (tab) => { console.log('当前激活标签:', tab.props.name) } </script> ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值