【发布时间】:2012-02-23 18:05:27
【问题描述】:
我想为我拥有的淡入/淡出文本功能的文本设置随机颜色
功能(有效):
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);
谢谢,
【问题讨论】:
-
将颜色放入一个数组中并使用 Math.random 选择一个。这是一篇关于从 Math.random shawnolson.net/a/789/make_javascript_mathrandom_useful.html获取整数的简洁小文章@