jQuery从给定列表中获取随机颜色 [英] jQuery get random color from a given list

查看:70
本文介绍了jQuery从给定列表中获取随机颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为fadeIn / Out文本函数的文本设置一个随机颜色

I would like to set a random color for the text of a fadeIn/Out text function I have

函数(有效):

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);

    }

);

并假设我想要颜色:

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

blue: #37fff5 green: #8cff04 orange: #ffa018 hot pink: #f247f8

虽然我知道我会怎么做将颜色添加到给定元素我不知道如何从我上面所见的solors中选择一个随机颜色:

Although I know how I would add the color to the given element I do not know how could I choose a random color from the solors I have lsited above:

更改文本的颜色淡入我会

To change the color of the text that is fading in I would

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

AND

jQuery("div.custom_logo").children().eq(currentItem).css('color', **NEWRandomColor**).fadeIn(fadeTime);

谢谢,

推荐答案

我认为这种方式是最好的

I think this way is the best

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

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

the return of the function will be the color example:

a = RandomColor();
console.log(a)//blue

a = RandomColor(); console.log(a) // "blue"

这篇关于jQuery从给定列表中获取随机颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆