要使用JavaScript实现发光效果,通常会结合CSS来完成。以下是一个简单的示例,展示如何创建一个带有发光效果的元素:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>发光效果示例</title>
<style>
.glow {
width: 200px;
height: 200px;
background-color: #3498db;
border-radius: 50%;
position: relative;
margin: 50px auto;
}
.glow::before {
content: '';
position: absolute;
top: -5px;
left: -5px;
right: -5px;
bottom: -5px;
background-color: #3498db;
border-radius: 50%;
z-index: -1;
filter: blur(10px);
opacity: 0.7;
animation: glow 2s ease-in-out infinite alternate;
}
@keyframes glow {
from {
filter: blur(10px) brightness(1.1);
}
to {
filter: blur(20px) brightness(1.3);
}
}
</style>
</head>
<body>
<div class="glow"></div>
</body>
</html>
div
元素,并赋予它一个类名glow
。.glow
:设置基本的样式,如大小、背景颜色、圆角等。.glow::before
:使用伪元素创建一个覆盖在原始元素上的层,通过模糊和透明度来模拟发光效果。@keyframes glow
:定义动画关键帧,使发光效果动起来。will-change
属性来优化性能。Modernizr
来处理兼容性问题。通过上述方法,你可以轻松地为网页元素添加一个吸引人的发光效果。
领取专属 10元无门槛券
手把手带您无忧上云