css中如何设置动态背景圆点完整代码
以下是一个使用CSS设置动态背景圆点的完整代码示例:
HTML代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="background"></div>
</body>
</html>
CSS代码(style.css):
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
.background::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(to bottom, rgba(255,255,255,0) 0%, rgba(255,255,255,0.8) 50%, rgba(255,255,255,0) 100%);
z-index: -1;
animation: animateBackground 5s linear infinite;
}
@keyframes animateBackground {
0% {
background-position: 0 0;
}
100% {
background-position: 0 -100%;
}
}
.background::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: radial-gradient(circle, #ffffff 10%, transparent 10%);
background-size: 30px 30px;
z-index: -1;
pointer-events: none;
}
这段代码中,首先使用一个div元素作为背景的容器,并添加一个名为background的类。
然后,使用::before伪元素来创建一个渐变的背景,使用linear-gradient函数设置从透明到半透明的渐变色。通过animation属性将背景动画化,并设置其持续时间为5秒,动画效果为线性无限循环滚动。
接下来,使用::after伪元素创建一个圆点背景。使用radial-gradient函数设置背景为圆点,并使用background-size属性设置圆点的大小。最后,通过pointer-events属性将该伪元素的点击事件禁用,以确保它不会干扰其他元素的交互。
原文地址: https://www.cveoy.top/t/topic/jepB 著作权归作者所有。请勿转载和采集!