• vue之tab栏切换


    一个简单的tab栏切换组件,由tabs以及tab-pane组成

    效果图
    在这里插入图片描述

    使用

    <template>
      <div class="container">
        <tabs
          default-active="2"
          class="build-tabs"
        >
          <tab-pane
            label="tab栏标题1"
            index="1"
          >tab栏内容1tab-pane>
          <tab-pane
            label="tab栏标题tab栏标题2"
            index="2"
          >
            tab栏内容2
          tab-pane>
          <tab-pane
            label="tab栏标题"
            index="3"
          >tab栏内容3tab-pane>
          <tab-pane
            label="标题"
            index="4"
          >tab栏内容4tab-pane>
          <tab-pane
            label="tab栏标题3"
            index="5"
          >tab栏内容5tab-pane>
        tabs>
      div>
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    tabs

    <template>
      <div
        v-show="pans.length"
        class="tabs"
      >
        <div class="tab-title">
          <div
            v-for="(item) in pans"
            :key="item.id"
            class="item"
            :class="{ 'active': currentActive === item.index }"
            @click="changeTab(item.index)"
          >{{ item.label }}div>
        div>
        <div class="tab-content">
          <slot>slot>
        div>
      div>
    template>
    
    <script>
    export default {
      props: {
        mode: {
          type: String,
          default: "horizontal/vertical"
        },
        defaultActive: {
          type: String | Number,
          default: '1'
        },
        defaultColor: {
          type: String,
          default: '#409EFF'
        }
      },
      data: () => {
        return {
          currentActive: '',
          pans: []
        }
      },
      computed: {
    
      },
      watch: {
        defaultActive: {
          handler (newVal) {
            this.currentActive = newVal
          },
          immediate: true
        }
      },
      mounted () {
      },
    
      methods: {
        changeTab (val) {
          this.currentActive = val
        },
      }
    }
    script>
    <style scoped lang="scss">
    ::root {
      --color: "#409EFF";
    }
    .tabs {
      .tab-title {
        display: flex;
        // align-items: flex-start;
        align-items: stretch;	// 侧边栏时,使侧栏高度与内容高度一致,按最高的显示
        margin-bottom: 14px;
        border-bottom: 1px solid #ccc;
        .item {
          padding: 20px;
          /* padding-bottom: 20px; */
          cursor: pointer;
          white-space: nowrap;
        }
        .active {
          // color: var(--color);
          color: #409EFF;
          /* padding-bottom: 15px; // 修正边框值:20px - 5px = 15px */
          border-bottom: 5px solid #409EFF;
          background-image: linear-gradient(
            to top,
            rgba($color: #409EFF, $alpha: 0.2),
            transparent
          );
        }
      }
    }
    style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    tab-pane

    <template>
      <div
        v-show="show && renderPan"
        class="tab-pane"
      >
        <slot>slot>
      div>
    template>
    <script>
    export default {
      name: 'tabPane',
      props: {
        index: {
          type: [String, Number],
          default: ''
        },
        label: {
          type: String,
          required: true
        }
      },
      data: () => {
        return {
          renderPan: false
        }
      },
      computed: {
        show () {
          if (this.$parent.currentActive === this.index) return true
          return false
        }
      },
      mounted () {
        this.$parent.pans.push({ id: Date.parse(new Date()) + Math.random(), index: this.index, label: this.label });
        this.renderPan = true
      },
      methods: {
      },
    }
    script>
    <style scoped lang="scss">
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
  • 相关阅读:
    LeetCode 42. 接雨水
    SkiaSharp 之 WPF 自绘 粒子花园(案例版)
    Win系统VMware虚拟机安装配置(一)(附激活码安装包)
    免费小程序商城搭建之b2b2c o2o 多商家入驻商城 直播带货商城 电子商务b2b2c o2o 多商家入驻商城 直播带货商城 电子商务
    我的创作纪念日
    git提交错了?别慌,直接删除提交记录
    Go中IO包简单使用
    Springboot企业工资管理系统r9a51计算机毕业设计-课程设计-期末作业-毕设程序代做
    【Java面试】面试如何让面试官面的很爽,看完这道面试题,finally块一定会执行吗?
    【项目】若依框架如何实现批量导入,并解析出表中内容返回给前端? - poi依赖
  • 原文地址:https://blog.csdn.net/wgh4318/article/details/126215160