jQuery从给定列表中获取随机颜色

Ibe*_*dia 2 random jquery

我想为我所拥有的淡入/淡出文本功能的文本设置一个随机颜色

功能(有效):

jQuery("div.custom_logo").ready(

    function(){

        //initial fade-in time (in milliseconds)
        var initialFadeIn = 1500;

        //interval between items (in milliseconds)
        var itemInterval = 2000;

        //cross-fade time (in milliseconds)
        var fadeTime = 500;

        //count number of items
        var numberOfItems = jQuery("div.custom_logo").children().length;

        //set current item
        var currentItem = 0;

        //show first item
        jQuery("div.custom_logo").children().eq(currentItem).fadeIn(initialFadeIn);

        //loop through the items
        var infiniteLoop = setInterval(function(){
            jQuery("div.custom_logo").children().eq(currentItem).fadeOut(fadeTime);

            if(currentItem == numberOfItems -1){
                currentItem = 0;
            }else{
                currentItem++;
            }
            jQuery("div.custom_logo").children().eq(currentItem).fadeIn(fadeTime);

        }, itemInterval);

    }

);
Run Code Online (Sandbox Code Playgroud)

假设我想要颜色:

蓝色:#37fff5绿色:#8cff04橙色:#ffa018粉红色:#f247f8

虽然我知道如何将颜色添加到给定元素中,但我不知道如何从我上面提到的solors中选择随机颜色:

要改变正在消失的文本的颜色,我会

//show first item
jQuery("div.custom_logo").children().eq(currentItem).css('color', theRandomColor).fadeIn(initialFadeIn);
Run Code Online (Sandbox Code Playgroud)

jQuery("div.custom_logo").children().eq(currentItem).css('color', **NEWRandomColor**).fadeIn(fadeTime);
Run Code Online (Sandbox Code Playgroud)

谢谢,

Ber*_*ire 11

我认为这种方式是最好的

RandomColor = function() {
    colors = ['red', 'white', 'blue', 'green']
    return colors[Math.floor(Math.random()*colors.length)];
}
Run Code Online (Sandbox Code Playgroud)

函数的返回将是颜色示例:

a = RandomColor(); console.log(a)//"蓝色"