模仿tabs标签页切换(简单封装-可以根据需求改)

<template>
    <div class="tabs">
        <div class="line"></div>
        <div ref="tabs" class="tab_item" :class="{active_item:ActiveIndex == index}" v-for="(item,index) in tabList" :key="index" @click="ActiveItem(index)">{{ item }}</div>
    </div>
</template>

<script>
export default {
    props:{
        TabIndex:{
            type:Number,
            default:()=>0
        }
    },
    data() {
        return {
            ActiveIndex: this.TabIndex,
            tabList: ["标签1", "标签测试2", "标签测试3", "标签字数测试4"],
        };
    },
    mounted() {
        this.ActiveItem(this.ActiveIndex);
    },
    methods: {
        ActiveItem(index) {
            let line = document.querySelector(".line");
            this.ActiveIndex = index;
            let countWidth = 0;
            line.style.width = `${this.$refs.tabs[index].offsetWidth}px`;
            if (index == 0) {
                countWidth = 0;
            } else {
                for (let i = 0; i < index; i++) {
                    countWidth += this.$refs.tabs[i].offsetWidth + 10;
                }
            }
            line.style.transform = `translateX(${countWidth}px)`;
        },
    },
    computed:{
    }
};
</script>

<style lang="scss" scoped>
.tabs {
    display: flex;
    position: relative;
    .tab_item {
        padding: 5px 10px;
        cursor: pointer;
        text-align: center;
        margin-left: 10px;
    }
    .active_item {
        color: #409eff;
    }
    .line {
        position: absolute;
        left: 10px;
        bottom: 0;
        height: 2px;
        background-color: #409eff;
        border-radius: 10px;
        transition: transform 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
    }
}
</style>

组件使用
TabIndex:默认选择的标签

<Tabs :TabIndex='1'></Tabs>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值