美文网首页
bootstrap4超简单的随机颜色标签云

bootstrap4超简单的随机颜色标签云

作者: 柚子_C | 来源:发表于2020-04-04 23:54 被阅读0次

效果:

效果

html:

……

<div class="w-100 tags">

<a href="link" class="badge badge-info p-2" style="background:#00557f">tagstext]</a>

<a href="link" class="badge badge-info p-2" style="background:#00557f">tagstext]</a>

<a href="link" class="badge badge-info p-2" style="background:#00557f">tagstext]</a>

<a href="link" class="badge badge-info p-2" style="background:#00557f">tagstext]</a>

</div>

……

jq代码

<script>

    var labelindex = $(".tags>a").length;

    var colorList = ["#9dc6eb", "#f8c471", "#b9a3ef", "#fdb1ca", "#9dc6eb", "#f8c471", "#b9a3ef", "#fdb1ca"];

    for (var i = 0; i < labelindex; i++) {

        var bgColor = getColorByRandom(colorList);

        $(".tags>a").eq(i).css("background", bgColor);

    }

    function getColorByRandom(colorList) {

        var colorIndex = Math.floor(Math.random() * colorList.length);

        var color = colorList[colorIndex];

        colorList.splice(colorIndex, 1);

        return color;

    }

</script>

相关文章

网友评论

      本文标题:bootstrap4超简单的随机颜色标签云

      本文链接:https://www.haomeiwen.com/subject/xfmephtx.html