css制作灯光
使用CSS制作灯光效果
通过CSS的box-shadow和filter属性可以模拟灯光效果,适用于按钮、图标或背景元素的光照增强。
基础灯光效果(发光按钮)
.glow-button {
background-color: #4CAF50;
border: none;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 8px;
box-shadow: 0 0 20px rgba(76, 175, 80, 0.6);
transition: box-shadow 0.3s ease;
}
.glow-button:hover {
box-shadow: 0 0 30px rgba(76, 175, 80, 0.9);
}
霓虹灯文字效果
结合text-shadow和动画实现闪烁霓虹灯:
.neon-text {
color: #fff;
text-shadow:
0 0 5px #fff,
0 0 10px #fff,
0 0 20px #ff00de,
0 0 40px #ff00de;
animation: flicker 1.5s infinite alternate;
}
@keyframes flicker {
0%, 19%, 21%, 23%, 25%, 54%, 56%, 100% {
text-shadow:
0 0 5px #fff,
0 0 10px #fff,
0 0 20px #ff00de,
0 0 40px #ff00de;
}
20%, 24%, 55% {
text-shadow: none;
}
}
聚光灯效果
使用径向渐变和混合模式创建聚焦光斑:
.spotlight {
position: relative;
height: 300px;
background: #222;
}
.spotlight::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
background: radial-gradient(
circle closest-side,
rgba(255,255,255,0.8),
transparent
);
transform: translate(-50%, -50%);
mix-blend-mode: overlay;
}
悬停灯光追踪
通过CSS变量实现鼠标悬停时的动态光照:
.light-track {
--x: 50%;
--y: 50%;
position: relative;
background: #333;
height: 200px;
overflow: hidden;
}
.light-track::after {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: radial-gradient(
circle at var(--x) var(--y),
rgba(255,255,255,0.3),
transparent 60%
);
pointer-events: none;
}
.light-track:hover::after {
background: radial-gradient(
circle at var(--x) var(--y),
rgba(255,255,255,0.6),
transparent 60%
);
}
JavaScript配合(可选):
document.querySelector('.light-track').addEventListener('mousemove', (e) => {
const rect = e.target.getBoundingClientRect();
e.target.style.setProperty('--x', `${e.clientX - rect.left}px`);
e.target.style.setProperty('--y', `${e.clientY - rect.top}px`);
});
3D灯泡效果
结合多重阴影和过渡创建立体灯泡:
.light-bulb {
width: 80px;
height: 80px;
background: #f5f5f5;
border-radius: 50%;
position: relative;
box-shadow:
inset 0 -5px 20px rgba(0,0,0,0.1),
inset 0 10px 30px rgba(255,255,255,0.8),
0 0 0 5px rgba(255,255,255,0.1),
0 5px 20px rgba(255,200,0,0.3);
transition: all 0.3s ease;
}
.light-bulb::after {
content: "";
position: absolute;
top: 15%;
left: 15%;
width: 70%;
height: 70%;
border-radius: 50%;
background: radial-gradient(circle at center, #fff, transparent 60%);
opacity: 0;
transition: opacity 0.3s ease;
}
.light-bulb.on {
box-shadow:
inset 0 -5px 20px rgba(0,0,0,0.1),
inset 0 10px 30px rgba(255,255,255,0.8),
0 0 0 5px rgba(255,255,255,0.1),
0 5px 50px rgba(255,200,0,0.8);
}
.light-bulb.on::after {
opacity: 0.6;
}






