<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>真实电风扇模拟</title>
<style>
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(to bottom, #e0e9f5, #f5f5f5);
height: 100vh;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
}
.fan-simulator {
position: relative;
width: 800px;
height: 600px;
}
/* 风扇主体 */
.fan-body {
position: absolute;
width: 400px;
height: 400px;
top: 50px;
left: 200px;
background-color: #e8e8e8;
border-radius: 200px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
overflow: hidden;
border: 10px solid #d0d0d0;
box-sizing: border-box;
}
/* 风扇网罩 - 前部 */
.fan-grill-front {
position: absolute;
width: 400px;
height: 400px;
border-radius: 200px;
background:
radial-gradient(circle at center, transparent 80px, rgba(180, 180, 180, 0.6) 81px),
repeating-conic-gradient(from 0deg,
rgba(100, 100, 100, 0.3) 0deg 1deg,
transparent 1deg 11deg);
z-index: 5;
pointer-events: none;
}
/* 风扇网罩 - 环形边框 */
.fan-grill-ring {
position: absolute;
width: 380px;
height: 380px;
top: 10px;
left: 10px;
border: 4px solid #b8b8b8;
border-radius: 200px;
box-sizing: border-box;
z-index: 6;
}
/* 风扇中心 */
.fan-center {
position: absolute;
width: 70px;
height: 70px;
background: radial-gradient(circle at center, #383838, #181818);
border-radius: 50%;
top: 165px;
left: 165px;
z-index: 4;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
border: 3px solid #505050;
box-sizing: border-box;
}
.fan-center::after {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: #b0b0b0;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
}
/* 风扇叶片容器 */
.fan-blades-container {
position: absolute;
width: 300px;
height: 300px;
top: 50px;
left: 50px;
z-index: 3;
transform-origin: center center;
}
/* 风扇叶片 */
.blade {
position: absolute;
width: 120px;
height: 40px;
background: linear-gradient(to right, #a0a0a0, #e0e0e0);
top: 130px;
left: 150px;
transform-origin: 0 50%;
border-radius: 5px 40% 40% 5px;
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
}
.blade::before {
content: '';
position: absolute;
width: 100%;
height: 100%;
background: linear-gradient(to right, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.1));
border-radius: inherit;
transform: skewY(-5deg);
}
.blade:nth-child(1) { transform: rotate(0deg); }
.blade:nth-child(2) { transform: rotate(60deg); }
.blade:nth-child(3) { transform: rotate(120deg); }
.blade:nth-child(4) { transform: rotate(180deg); }
.blade:nth-child(5) { transform: rotate(240deg); }
.blade:nth-child(6) { transform: rotate(300deg); }
/* 风扇支架 */
.fan-stand {
position: absolute;
width: 40px;
height: 150px;
background: linear-gradient(to right, #808080, #a0a0a0, #808080);
top: 400px;
left: 380px;
border-radius: 5px;
box-shadow: -2px 10px 10px rgba(0, 0, 0, 0.1);
}
/* 风扇底座 */
.fan-base {
position: absolute;
width: 200px;
height: 30px;
background: linear-gradient(to bottom, #a0a0a0, #707070);
top: 550px;
left: 300px;
border-radius: 15px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}
/* 控制面板 */
.control-panel {
position: absolute;
width: 240px;
height: 120px;
background: linear-gradient(to bottom, #d0d0d0, #b0b0b0);
top: 420px;
left: 280px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
padding: 10px;
border: 1px solid #909090;
}
/* 风扇控制部分 */
.control-row {
display: flex;
justify-content: space-around;
width: 100%;
align-items: center;
}
/* 电源开关 */
.power-switch {
position: relative;
width: 60px;
height: 30px;
margin: 10px;
}
.power-switch input {
opacity: 0;
width: 0;
height: 0;
}
.power-slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #707070;
transition: .4s;
border-radius: 34px;
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
}
.power-slider:before {
position: absolute;
content: "";
height: 22px;
width: 22px;
left: 4px;
bottom: 4px;
background-color: #f0f0f0;
transition: .4s;
border-radius: 50%;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
}
input:checked + .power-slider {
background-color: #66bb6a;
}
input:checked + .power-slider:before {
transform: translateX(30px);
}
/* 速度控制旋钮 */
.speed-control {
position: relative;
width: 70px;
height: 70px;
}
.knob {
position: absolute;
width: 70px;
height: 70px;
border-radius: 50%;
background: radial-gradient(circle at 40% 40%, #f0f0f0, #c0c0c0);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
cursor: pointer;
transform-origin: center center;
transform: rotate(0deg);
transition: transform 0.3s ease-out;
border: 1px solid #909090;
}
.knob::before {
content: "";
position: absolute;
width: 4px;
height: 20px;
background-color: #505050;
top: 5px;
left: 50%;
transform: translateX(-50%);
border-radius: 2px;
}
.speed-marks {
position: absolute;
width: 100%;
height: 100%;
pointer-events: none;
}
.mark {
position: absolute;
width: 4px;
height: 10px;
background-color: #505050;
border-radius: 2px;
transform-origin: bottom center;
}
.mark:nth-child(1) {
transform: translateX(-2px) rotate(-60deg) translateY(-30px);
}
.mark:nth-child(2) {
transform: translateX(-2px) rotate(-30deg) translateY(-30px);
}
.mark:nth-child(3) {
transform: translateX(-2px) rotate(0deg) translateY(-30px);
}
.mark:nth-child(4) {
transform: translateX(-2px) rotate(30deg) translateY(-30px);
}
.mark:nth-child(5) {
transform: translateX(-2px) rotate(60deg) translateY(-30px);
}
.speed-labels {
position: absolute;
width: 150px;
top: 75px;
left: -40px;
display: flex;
justify-content: space-between;
font-size: 12px;
color: #505050;
}
/* 指示灯 */
.indicator-light {
width: 12px;
height: 12px;
border-radius: 50%;
background-color: #707070;
box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.5);
transition: all 0.3s;
}
.indicator-light.on {
background-color: #66bb6a;
box-shadow: 0 0 10px #66bb6a, inset 0 0 3px rgba(0, 0, 0, 0.3);
}
.speed-indicator {
display: flex;
justify-content: space-between;
width: 180px;
}
.speed-indicator .indicator-light {
margin: 0 5px;
}
/* 速度标签 */
.speed-text {
margin-top: 10px;
color: #505050;
font-size: 14px;
font-weight: bold;
}
/* 旋转动画 */
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.speed-off {
animation: none;
transform: rotate(0deg) !important;
}
.speed-1 {
animation: rotate 4s linear infinite;
}
.speed-2 {
animation: rotate 2s linear infinite;
}
.speed-3 {
animation: rotate 1s linear infinite;
}
.speed-4 {
animation: rotate 0.6s linear infinite;
}
/* 启动和停止动画 */
@keyframes startup {
from { animation-duration: 4s; }
to { animation-duration: var(--target-duration); }
}
@keyframes shutdown {
from { animation-duration: var(--current-duration); }
to { animation-duration: 10s; opacity: 1; }
99% { opacity: 1; }
100% { opacity: 0; animation: none; }
}
.startup {
animation-name: rotate, startup;
animation-duration: 4s, 3s;
animation-timing-function: linear, ease-in;
animation-iteration-count: infinite, 1;
animation-fill-mode: forwards;
}
.shutdown {
animation-name: rotate, shutdown;
animation-timing-function: linear, ease-out;
animation-iteration-count: infinite, 1;
animation-fill-mode: forwards;
animation-duration: var(--current-duration), 3s;
}
</style>
</head>
<body>
<div class="fan-simulator">
<!-- 风扇主体 -->
<div class="fan-body">
<!-- 风扇叶片 -->
<div class="fan-blades-container" id="blades">
<div class="blade"></div>
<div class="blade"></div>
<div class="blade"></div>
<div class="blade"></div>
<div class="blade"></div>
<div class="blade"></div>
</div>
<!-- 风扇中心 -->
<div class="fan-center"></div>
<!-- 风扇网罩 -->
<div class="fan-grill-front"></div>
<div class="fan-grill-ring"></div>
</div>
<!-- 风扇支架和底座 -->
<div class="fan-stand"></div>
<div class="fan-base"></div>
<!-- 控制面板 -->
<div class="control-panel">
<div class="control-row">
<!-- 电源开关 -->
<div class="power-container">
<label class="power-switch">
<input type="checkbox" id="power-toggle">
<span class="power-slider"></span>
</label>
<div class="speed-text">电源</div>
</div>
<!-- 速度控制旋钮 -->
<div class="speed-control">
<div class="knob" id="speed-knob">
<div class="speed-marks">
<div class="mark"></div>
<div class="mark"></div>
<div class="mark"></div>
<div class="mark"></div>
<div class="mark"></div>
</div>
</div>
<div class="speed-labels">
<span>0</span>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
<div class="speed-text">风速</div>
</div>
</div>
<!-- 指示灯 -->
<div class="speed-indicator">
<div class="indicator-light" id="power-light"></div>
<div class="indicator-light" id="speed-light-1"></div>
<div class="indicator-light" id="speed-light-2"></div>
<div class="indicator-light" id="speed-light-3"></div>
<div class="indicator-light" id="speed-light-4"></div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const blades = document.getElementById('blades');
const powerToggle = document.getElementById('power-toggle');
const speedKnob = document.getElementById('speed-knob');
const powerLight = document.getElementById('power-light');
const speedLights = [
document.getElementById('speed-light-1'),
document.getElementById('speed-light-2'),
document.getElementById('speed-light-3'),
document.getElementById('speed-light-4')
];
let currentSpeed = 0;
let isOn = false;
let knobAngle = 0;
// 动画持续时间对应表(秒)
const durationMap = {
0: 0,
1: 4,
2: 2,
3: 1,
4: 0.6
};
// 更新风扇状态
function updateFanState() {
// 移除所有风扇速度类
blades.className = 'fan-blades-container';
// 设置当前风扇状态
if (isOn) {
if (currentSpeed === 0) {
isOn = false;
powerToggle.checked = false;
updateLights();
return;
}
if (blades.classList.contains('shutdown')) {
blades.classList.remove('shutdown');
}
// 启动动画
blades.style.setProperty('--target-duration', `${durationMap[currentSpeed]}s`);
blades.classList.add('startup');
// 启动动画结束后切换到正常速度
setTimeout(() => {
blades.classList.remove('startup');
blades.classList.add(`speed-${currentSpeed}`);
}, 3000);
} else {
if (currentSpeed > 0) {
// 关闭动画
blades.style.setProperty('--current-duration', `${durationMap[currentSpeed]}s`);
blades.classList.remove(`speed-${currentSpeed}`);
blades.classList.add('shutdown');
// 关闭动画结束后移除类
setTimeout(() => {
blades.classList.remove('shutdown');
blades.classList.add('speed-off');
}, 3000);
} else {
blades.classList.add('speed-off');
}
}
updateLights();
}
// 更新指示灯状态
function updateLights() {
// 电源灯
powerLight.classList.toggle('on', isOn);
// 速度灯
speedLights.forEach((light, index) => {
light.classList.toggle('on', isOn && index < currentSpeed);
});
}
// 电源开关
powerToggle.addEventListener('change', function() {
isOn = this.checked;
if (isOn && currentSpeed === 0) {
currentSpeed = 1;
knobAngle = -30;
speedKnob.style.transform = `rotate(${knobAngle}deg)`;
}
updateFanState();
});
// 速度旋钮控制
let isDragging = false;
let startAngle = 0;
let startKnobAngle = 0;
speedKnob.addEventListener('mousedown', function(e) {
isDragging = true;
// 计算鼠标相对于旋钮中心的位置
const knobRect = speedKnob.getBoundingClientRect();
const knobCenterX = knobRect.left + knobRect.width / 2;
const knobCenterY = knobRect.top + knobRect.height / 2;
// 计算初始角度
startAngle = Math.atan2(e.clientY - knobCenterY, e.clientX - knobCenterX) * 180 / Math.PI;
startKnobAngle = knobAngle;
document.addEventListener('mousemove', handleKnobDrag);
document.addEventListener('mouseup', stopKnobDrag);
// 防止拖动时选中文本
e.preventDefault();
});
function handleKnobDrag(e) {
if (!isDragging) return;
// 计算鼠标相对于旋钮中心的位置
const knobRect = speedKnob.getBoundingClientRect();
const knobCenterX = knobRect.left + knobRect.width / 2;
const knobCenterY = knobRect.top + knobRect.height / 2;
// 计算当前角度
const currentAngle = Math.atan2(e.clientY - knobCenterY, e.clientX - knobCenterX) * 180 / Math.PI;
let angleDiff = currentAngle - startAngle;
// 计算新的旋钮角度
let newKnobAngle = startKnobAngle + angleDiff;
// 限制旋钮旋转范围
newKnobAngle = Math.max(-60, Math.min(60, newKnobAngle));
// 应用旋钮旋转
knobAngle = newKnobAngle;
speedKnob.style.transform = `rotate(${knobAngle}deg)`;
// 根据旋钮角度设置速度
if (knobAngle < -45) {
currentSpeed = 0;
} else if (knobAngle < -15) {
currentSpeed = 1;
} else if (knobAngle < 15) {
currentSpeed = 2;
} else if (knobAngle < 45) {
currentSpeed = 3;
} else {
currentSpeed = 4;
}
// 更新风扇状态
updateFanState();
}
function stopKnobDrag() {
if (!isDragging) return;
isDragging = false;
document.removeEventListener('mousemove', handleKnobDrag);
document.removeEventListener('mouseup', stopKnobDrag);
// 自动吸附到最近的速度级别
const targetAngles = [-60, -30, 0, 30, 60];
let closestAngle = targetAngles[0];
let minDiff = Math.abs(knobAngle - targetAngles[0]);
targetAngles.forEach(angle => {
const diff = Math.abs(knobAngle - angle);
if (diff < minDiff) {
minDiff = diff;
closestAngle = angle;
}
});
// 应用吸附
knobAngle = closestAngle;
speedKnob.style.transform = `rotate(${knobAngle}deg)`;
// 设置对应的速度
currentSpeed = (knobAngle + 60) / 30;
// 更新风扇状态
updateFanState();
}
// 初始化
updateFanState();
});
</script>
</body>
</html>