uniapp的tab栏切换

<template>
    <view class="tab">
        <view class="tab-item" @click="testTabClick(index)" :class="tabIndex == index?'active':''" v-for="(item,index) in tabList" :key="index">
            {
    
    {item.name}}
        </view>
    </view>
</template>
<script>
    export default{
        data() {
            return {
                tabIndex:0,
                tabList:[{
                    name:"电器"
                },{
                    name:"衣服"
                },{
                    name:"百货"
                },{
                    name:"电脑"
                }]
            }
        },
        methods: {
            testTabClick(index) {
                console.log(index)
                this.tabIndex = index
            }
        }
    }
</script>
<style lang="scss" scoped>
    .tab{
        display: flex;
        .tab-item{
            flex-shrink: 0;
            padding: 24rpx;
            position: relative;
            transition: all 0.2s linear;
            &::after{
                transition: all 0.2s linear;
                transform: translateX(-50%) scaleX(0);
                content: '';
                width: 50%;
                position: absolute;
                left: 50%;
                bottom: 20rpx;
                border-bottom: 6rpx solid red;
                border-radius: 4rpx;
            }
            &.active{
                &::after{
                    content: '';
                    width: 50%;
                    position: absolute;
                    left: 50%;
                    transform: translateX(-50%) scaleX(1);
                    bottom: 20rpx;
                    border-bottom: 6rpx solid red;
                }
            }
        }
    }
</style>

猜你喜欢

转载自blog.csdn.net/m0_45865109/article/details/129137346