描述:使用css3动画使四个盒子旋转,每隔500ms随机一个div改变成随机背景色, 利用 rgb(255, 255, 255)
技术点
1.CSS3旋转
2.随机改变颜色
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>盒子旋转变色</title>
<style>
#wrap {
width: 500px;
height: 500px;
margin: 10px auto;
position: relative;
}
#wrap .position {
position: absolute;
left: 50%;
top: 50%;
}
#wrap .box4 {
width: 400px;
height: 400px;
background-color: #DB4A82;
margin-top: -200px;
margin-left: -200px;
animation: clockwise 3s linear infinite;
}
#wrap .box3 {
width: 300px;
height: 300px;
background-color: #E1EEBD;
margin-top: -150px;
margin-left: -150px;
animation: anticlockwise 3s linear infinite;
}
#wrap .box2 {
width: 200px;
height: 200px;
background-color: #C46DBE;
margin-top: -100px;
margin-left: -100px;
animation: clockwise1 3s linear infinite;
}
#wrap .box1 {
width: 100px;
height: 100px;
background-color: #C7CB8E;
margin-top: -50px;
margin-left: -50px;
animation: anticlockwise 3s linear infinite;
}
@keyframes clockwise {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(180deg);
}
100% {
transform: rotate(360deg);
}
}
@keyframes clockwise1 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(360deg);
}
100% {
transform: rotate(720deg);
}
}
@keyframes anticlockwise {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-360deg);
}
100% {
transform: rotate(-720deg);
}
}
</style>
</head>
<body>
<div id="wrap">
<div class="box4 position">
<div class="box3 position">
<div class="box2 position">
<div class="box1 position"></div>
</div>
</div>
</div>
</div>
<script src="./jquery.js"></script>
<script>
setInterval(function () {
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
var bgColor = "rgb(" + r + "," + g + "," + b + ")";
var index = Math.floor(Math.random() * ($('div').length - 1) + 1);
$('div').eq(index).css('background', bgColor);
}, 500);
</script>
</body>
</html>