uniapp+vue3+TypeScrit 自定义tabs

183 阅读1分钟

image.png

image.png

<template>
    <view class="container">
        <scroll-view class="scroll-view_H" :style="scrollStyle" scroll-x="true" scroll-left="0">
            <view class="list" :style="listStyle">
                <view :class="item.value === current ? 'normal-tabs tabs' : 'normal-tabs'" v-for="(item, index) in tabsList" :key="index" @click="onChangeTabs(item)">
                    <view class="tabs-content">
                        <view v-if="item.icon">
                            <image class="tabs-img" :src="item.icon"></image>
                        </view>
                        <view>{{ item.label }}</view>
                    </view>
                </view>
            </view>
        </scroll-view>
    </view>
</template>

<script setup lang="ts">
import { ref, reactive, defineProps, defineEmits } from 'vue';
interface IProps {
    tabsList: Array<{
        value: number;
        label: string;
        icon?: string;
    }>;
    listStyle?: string;
    scrollStyle?: string;
}
const emit = defineEmits(['change']);
const props = withDefaults(defineProps<IProps>(), {
    tabsList: () => {
        return [];
    },
    scrollStyle: '',
});

const current = ref(props.tabsList[0].value); //当前tabBar栏
// 切换tabBar时
const onChangeTabs = (e: any) => {
    current.value = e.value;
    emit('change', e.value);
};
</script>

<style lang="scss" scoped>
.container {
    position: sticky;
    top: 0rpx;
    background: #fff;
    z-index: 9999;
}
.scroll-view_H {
    white-space: nowrap;
    width: 100%;
    height: 100rpx;
    border-bottom: 1rpx solid #f6f6f6;
    // background: #c9a1a1;
    ::-webkit-scrollbar {
        display: none;
        width: 0 !important;
        height: 0 !important;
        -webkit-appearance: none;
        background: transparent;
        color: transparent;
    }

    .list {
        width: 100%;
        height: 100% !important;
        display: inline-block;
        display: flex;
        align-items: center;
        color: #1e2732;
    }
    .normal-tabs {
        padding-left: 25rpx;
        padding-right: 25rpx;
    }
    .tabs {
        font-weight: bolder;
    }
    .tabs::after {
        content: '';
        width: 50%;
        height: 5rpx;
        display: block;
        margin: 0 auto;
        margin-top: 6rpx;
        background-color: #1e2732;
        border-radius: 30px;
    }
    .tabs-content {
        text-align: center;
    }
    .tabs-img {
        height: 50rpx;
        width: 50rpx;
        border-radius: 10rpx;
    }
}
</style>