.fan { width: 200px; height: 200px; position: relative; margin: 50px auto; border: 1px solid #ccc; border-radius: 50%; overflow: hidden; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);}.blade { position: absolute; top: 50%; left: 50%; margin: -75px 0 0 -5px; width: 10px; height: 150px; border-radius: 10px; background-color: #ccc; transform-origin: bottom center;}.blade1 { transform: rotate(0deg);}.blade2 { transform: rotate(90deg);}.blade3 { transform: rotate(180deg);}.blade4 { transform: rotate(270deg);}.fan:hover .blade { animation: rotate 1s linear infinite;}@keyframes rotate { from { transform: rotate(0deg); } to { transform: rotate(360deg); }}完善这串代码
时间: 2024-03-16 14:44:11 浏览: 63
Prometheus:自主无人机的开源软件
这段代码已经可以实现风扇的旋转效果,但是可以继续完善,例如添加过渡效果和动态调整旋转速度。以下是完善后的代码:
HTML代码:
```html
<div class="fan">
<div class="blade blade1"></div>
<div class="blade blade2"></div>
<div class="blade blade3"></div>
<div class="blade blade4"></div>
</div>
```
CSS代码:
```css
.fan {
width: 200px;
height: 200px;
position: relative;
margin: 50px auto;
border: 1px solid #ccc;
border-radius: 50%;
overflow: hidden;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
transition: all 0.3s ease-in-out;
}
.blade {
position: absolute;
top: 50%;
left: 50%;
margin: -75px 0 0 -5px;
width: 10px;
height: 150px;
border-radius: 10px;
background-color: #ccc;
transform-origin: bottom center;
}
.blade1 {
transform: rotate(0deg);
}
.blade2 {
transform: rotate(90deg);
}
.blade3 {
transform: rotate(180deg);
}
.blade4 {
transform: rotate(270deg);
}
.fan:hover .blade {
animation: rotate 1s linear infinite;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
```
JavaScript代码:
```javascript
var fan = document.querySelector('.fan');
var blades = document.querySelectorAll('.blade');
fan.addEventListener('mouseenter', function() {
blades.forEach(function(blade) {
var speed = Math.random() * 2 + 1; // 随机生成旋转速度
blade.style.animation = 'rotate ' + speed + 's linear infinite'; // 动态设置旋转速度
});
});
fan.addEventListener('mouseleave', function() {
blades.forEach(function(blade) {
blade.style.animation = '';
});
});
```
这段代码添加了过渡效果,并且可以动态调整旋转速度。当鼠标进入风扇时,会随机生成每个叶片的旋转速度,并且动态设置动画的属性。当鼠标离开风扇时,重置动画属性。你可以根据需要调整过渡效果和旋转速度的范围。
阅读全文