gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:03:46 24 4
gpt4 key购买 nike

我想为我拥有的淡入/淡出文本函数的文本设置随机颜色

功能(有效):

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

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

要更改淡入文本的颜色,我会

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

并且

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

谢谢,

最佳答案

我认为这种方式是最好的

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

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

a = 随机颜色();console.log(a)//“蓝色”

关于jQuery 从给定列表中获取随机颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9089396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com