我有这个函数返回一个随机颜色,但我想将它应用于jQuery .animate()
函数。
我该怎么做?
var colors = ["rgb(120,25,25)", "rgb(50,100,130)", "rgb(30,95,45)", "rgb(55,30,90)"];
function randomBackground() {
return colors[Math.floor(Math.random() * messages.length)];
}
$("#menu").animate({background: randomBackground()});
答案 0 :(得分:2)
你可以这样做:
var colors = ["rgb(120,25,25)", "rgb(50,100,130)", "rgb(30,95,45)", "rgb(55,30,90)"];
function randomBackground() {
return colors[Math.floor(Math.random() * colors.length)];
//use colors.length, not messages.length
}
var bgcolor = randomBackground();
$("#menu").animate({background: bgcolor });
答案 1 :(得分:1)
对不起,但如果您阅读了jQuery文档:
除非如下所述,否则所有动画属性都应设置为单个数值。大多数非数字属性无法使用基本jQuery功能进行动画处理。 (例如,宽度,高度或左侧可以设置动画,但背景颜色不能。)除非另有说明,否则属性值将被视为多个像素。可以在适用的地方指定单位em和%。
使用animate()
,你无法进行背景颜色变化