给循环里的属性前三个加不同的样式


<template>
    <view class="search-hot">
        <view class="search-hot-title" v-show="hotWord.length > 0">热词</view>
        <view class="search-hot-list">
            <view
                class="search-hot-list-item"
                v-for="(item, index) in hotWord"
                :key="index"
                @click="clickHot(item, index)"
                :class="[activeIndex == index ? 'active' : '']"
            >
                <span class="sort-style" :class="[`background_${item.sort > 4 ? 4 : item.sort}`]">{{ item.sort }}</span>
                {{ item.keyword }}
            </view>
        </view>
    </view>
</template>
<script>
// searchKeyword
export default {
    name: "hot-search",

    data() {
        return {
            hotWord: [],
            activeIndex: 0,
        };
    },
    created() {
        this.hotSearch();
    },
    methods: {
        clickHot(item, index) {
            this.activeIndex = index;
            this.$emit("searchHot", item);
        },
        hotSearch() {
            this.$d.api.market.getKeyWords().then(res => {
                console.log(res, "res");
                this.hotWord = res;
            });
        },
    },
};
</script>

<style scoped lang="scss">
.search-hot {
    // flex: 1;
    padding: 29rpx;
    display: flex;
    flex-direction: column;
    background: #fff;

    &-title {
        font-family: PingFangSC, PingFang SC;
        font-weight: 500;
        font-size: 25rpx;
        color: $d-main-color;
        line-height: 36rpx;
    }

    &-list {
        display: flex;
        flex-wrap: wrap;
        flex-direction: row;

        &-item {
            background: #eef0f5;
            border-radius: 7rpx;
            padding: 18rpx;
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 25rpx;
            color: $d-main-color;
            line-height: 36rpx;
            margin-right: 18rpx;
            margin-top: 18rpx;
            display: flex;
            justify-content: center;
            align-items: center;
            &:last-child {
                margin-right: 0;
            }
            .sort-style {
                padding: 0 3px;
                color: #ffffff;
                display: inline-block;
                margin: 0 5px;
                height: 13px;
                line-height: 13px;
                font-size: 10px;
            }
            .background_1 {
                background: green;
            }
            .background_2 {
                background: blue;
            }
            .background_3 {
                background: #f76560;
            }
            .background_4 {
                background: #ccc;
            }
        }
    }
}
</style>

通过添加动态样式去控制前三个元素的不同样式,第四个样式以后走一个默认样式 

 :class="[`background_${item.sort > 4 ? 4 : item.sort}`]"

 .background_1 {
                background: green;
            }
            .background_2 {
                background: blue;
            }
            .background_3 {
                background: #f76560;
            }
            .background_4 {
                background: #ccc;
            } 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值