开关灯效果
项目来源于抖音博主 - 前端斌少
预览
此演示仅在桌面端浏览器中可用
html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>开关灯效果</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<div class="container">
<div class="bulb">
<div class="filaments"></div>
</div>
</div>
<div class="floor">
<div class="shadow"></div>
</div>
<script src="./script.js"></script>
</body>
</html>css
css
body {
display: flex;
justify-content: center;
align-items: center;
background-color: #001933;
color: #fff;
}
.container {
width: 75px;
height: 275px;
position: absolute;
top: 0;
animation: bulb-swing 3s infinite ease-in-out;
transform-origin: top center;
}
.bulb {
z-index: 10;
display: block;
width: 75px;
height: 75px;
border-radius: 50%;
position: absolute;
top: 200px;
cursor: pointer;
background-color: #ffd700;
box-shadow: 5px 5px 80px gold, 5px -5px 80px gold, -5px 5px 80px gold, -5px -5px
80px gold;
}
.bulb::before {
content: "";
position: absolute;
width: 35px;
height: 50px;
background-color: #000;
bottom: 100%;
left: calc(50% - (35px / 2));
border-top-left-radius: 10px;
border-top-right-radius: 10px;
z-index: -1;
}
.bulb::after {
content: "";
position: absolute;
width: 10px;
height: 250px;
background-color: #000;
bottom: 100%;
left: calc(50% - (10px / 2));
z-index: -1;
}
.bulb.off {
background: transparent;
box-shadow: none;
border: 2px solid #000;
}
.filaments {
display: block;
position: absolute;
width: 2px;
height: 30px;
background-color: #000;
top: 2px;
left: 28px;
box-shadow: 17px 0 #000;
opacity: 0;
}
.filaments::after {
content: "";
display: block;
width: 2px;
height: 12px;
background-color: #000;
box-shadow: 4.5px 0 #000, 9px 0 #000;
position: absolute;
bottom: -1px;
left: 4px;
}
.filaments.off {
opacity: 1;
}
@keyframes bulb-swing {
0% {
transform: rotate(-10deg);
}
50% {
transform: rotate(10deg);
}
100% {
transform: rotate(-10deg);
}
}
.floor {
position: absolute;
background-color: #000c19;
height: 200px;
width: 100%;
bottom: 0;
left: 0;
}
.shadow {
position: absolute;
width: 50px;
height: 50px;
background-color: #524c11;
border-radius: 50%;
box-shadow: 20px 20px 100px gold, -20px 20px 100px gold, 20px -20px 100px gold,
-20px -20px 100px gold;
top: calc(50% - (50px / 2));
left: calc(50% - (50px / 2));
animation: shadow-swing 3s infinite ease-in-out;
}
.shadow.off {
background-color: #000c19;
box-shadow: none;
}
@keyframes shadow-swing {
0% {
transform: translateX(100px);
}
50% {
transform: translateX(-100px);
}
100% {
transform: translateX(100px);
}
}js
js
const $shadow = document.querySelector(".shadow");
const $light = document.querySelector(".bulb");
const $filaments = document.querySelector(".filaments");
$light.addEventListener("click", () => {
$light.classList.toggle("off");
$shadow.classList.toggle("off");
$filaments.classList.toggle("off");
});