简易的tab切换封装

Vue Tab切换组件简单封装,满足自己简单的功能,可以直接拿去使用!

<template>
    <div class="tab-list" ref="tabList">
         <div v-for="(item,index) in valueList" :key="index" class="tab-item" @click="itemClick(index)"
         :style="{width: tabItemWidth + 'px', color: currentIndex === index ? activeColor : '#333333'}" >
            {{item.name}}
         </div>
       <div class="bottom-line" :style="{width: tabItemWidth / 3 + 'px', left: lineLeft, backgroundColor: activeColor}" ></div>
    </div>
</template>

<script>
export default {
    props: {
        valueList: {
            type: Array,
            default() { return [] }
        },
        defalutIndex: {
            type: Number,
            default: 0
        },
        needUnderline: {
            type: Boolean,
            default: true
        },
        needBorderLine: {
            type: Boolean,
            default: true
        },
        background: {
            type: String,
            default: 'white'
        },
        activeColor: {
            type: String,
            default: '#3292FF'
        }
	},
    data() {
        return {
            tabItemWidth:200,
            currentIndex:0
        };
    },

    mounted() {
      this.initView()
    },
    computed: {
		lineLeft() {
			const lineWidth = this.tabItemWidth / 3
			const left = this.currentIndex * this.tabItemWidth + this.tabItemWidth / 2 - lineWidth / 2
			return left + 'px'
		}
	},
    methods: {
        initView(){
            this.currentIndex = this.defalutIndex
            this.tabItemWidth = this.$refs.tabList.offsetWidth / this.valueList.length
        },
        itemClick(index) {
			this.currentIndex = index
			this.$emit('change', index)
		}
    },
};
</script>

<style scoped>
.tab-list{background: #fff;}
.tab-list {width: 100%;height: 44px;position: relative;display: flex;justify-content: space-around;
		align-items: center;}
.tab-list .tab-item {height: 43px;line-height: 43px;font-size: 15px;font-weight: bold;text-align: center;transition-duration: 0.3s;}
.tab-list .bottom-line {height: 3px;border-radius: 4px;position: absolute;bottom: 0;transition-duration: 0.3s;}
</style>

可以通过keep-alive 保留数据值 填写数据时切换到其他页面,后返回当前页数据保留 ,主要用于保留组件状态或避免重新渲染

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值