Javascript 尝试在鼠标进入时随机化div颜色
我正在尝试使用它,以便在单击“随机化”按钮后,当用户将鼠标悬停在每个方块上时,每个方块都是不同的颜色。当我选择随机函数时,没有任何变化。这是我的密码:Javascript 尝试在鼠标进入时随机化div颜色,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在尝试使用它,以便在单击“随机化”按钮后,当用户将鼠标悬停在每个方块上时,每个方块都是不同的颜色。当我选择随机函数时,没有任何变化。这是我的密码: function randomSetup(numOfSquares){ var numSquares = numOfSquares; var squareSide = 500 / numSquares; var totalSquares = numSquares * numSquares; for(var r
function randomSetup(numOfSquares){
var numSquares = numOfSquares;
var squareSide = 500 / numSquares;
var totalSquares = numSquares * numSquares;
for(var rows = 0; rows < totalSquares; rows++){
$('<div class="gridSquare"></div>').appendTo('.container')
}
var colors = randomColor();
$('.container').on('mouseenter', '.gridSquare', function(){
$(this).css('background-color', 'rgb(colors,colors,colors)');
});
$('.gridSquare').width(squareSide);
$('.gridSquare').height(squareSide);
}
function randomColor(){
return Math.random() * (255 - 0) + 1;
}
函数随机设置(numOfSquares){
var numSquares=numOfSquares;
var squareSide=500/平方米;
var totalSquares=numSquares*numSquares;
对于(var行=0;行
这是无效的:
'rgb(colors,colors,colors)'
试试这个:
$('.container').on('mouseenter', '.gridSquare', function(){
var red = randomColor();
var green = randomColor();
var blue = randomColor();
$(this).css('background-color', 'rgb('+red+','+green+','+blue+')');
});
您还需要对随机数进行四舍五入:
function randomColor(){
return Math.round(Math.random() * (255 - 0) + 1);
}
这是无效的:
'rgb(colors,colors,colors)'
试试这个:
$('.container').on('mouseenter', '.gridSquare', function(){
var red = randomColor();
var green = randomColor();
var blue = randomColor();
$(this).css('background-color', 'rgb('+red+','+green+','+blue+')');
});
您还需要对随机数进行四舍五入:
function randomColor(){
return Math.round(Math.random() * (255 - 0) + 1);
}
给你:
特里皮
重新生成事件处理程序中的随机颜色,使每个方块都是不同的颜色
$('.container').on('mouseenter', '.gridSquare', function(){
var colors = [];
colors[0] = Math.round(randomColor());
colors[1] = Math.round(randomColor());
colors[2] = Math.round(randomColor());
$(this).css('background-color', 'rgb('+colors[0]+','+colors[1]+','+colors[2]+')');
});
给你:
特里皮
重新生成事件处理程序中的随机颜色,使每个方块都是不同的颜色
$('.container').on('mouseenter', '.gridSquare', function(){
var colors = [];
colors[0] = Math.round(randomColor());
colors[1] = Math.round(randomColor());
colors[2] = Math.round(randomColor());
$(this).css('background-color', 'rgb('+colors[0]+','+colors[1]+','+colors[2]+')');
});
你可以试试这样的东西
function randomColor(){
var colors = ["#CCCCCC","#333333","#990099"];
var rand = Math.floor(Math.random()*colors.length);
//return Math.random() * (255 - 0) + 1;
return colors[rand];
}
及
小提琴:你可以试试
function randomColor(){
var colors = ["#CCCCCC","#333333","#990099"];
var rand = Math.floor(Math.random()*colors.length);
//return Math.random() * (255 - 0) + 1;
return colors[rand];
}
及
小提琴:这样每个方块都有不同的颜色,所有的瓷砖都有相同的颜色,每个方块都有不同的颜色,所有的瓷砖都有相同的颜色可能重复的
randomColor
应该是randomNumber
,因为它只返回一个数字,而不是真正的颜色可能重复的颜色randomColor
可能应该是randomNumber
,因为它只返回一个数字而不是一个真实的颜色。为什么要使用数组?而Math.round()
调用应该在randomColor
中进行,为什么是数组?而Math.round()
调用应该在randomColor