**制作一个类似下图的网页背景** ![](/i/l/?n=24&i=blog/3557270/202412/3557270-20241204150004390-1380251371.png)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
list-style: none;
}
body{
background-color: #000;
}
span{
width: 30px;
height: 30px;
background: url("images/star.png") no-repeat;
position: absolute;
background-size:100% 100%;
animation: flash 1s alternate infinite;
}
@keyframes flash {
0%{opacity: 0;}
100%{opacity: 1;}
}
span:hover{
transform: scale(3, 3) rotate(180deg) !important;
transition: all 1s;
}
</style>
</head>
<body>
<script>
window.onload = function () {
// 1. 求出屏幕的尺寸
var screenW = document.documentElement.clientWidth;
var screenH = document.documentElement.clientHeight;
// 2. 动态创建星星
for(var i=0; i<150; i++){
// 2.1 创建星星
var span = document.createElement('span');
document.body.appendChild(span);
// 2.2 随机的坐标
var x = parseInt(Math.random() * screenW);
var y = parseInt(Math.random() * screenH);
span.style.left = x + 'px';
span.style.top = y + 'px';
// 2.3 随机缩放
var scale = Math.random() * 1.5;
span.style.transform = 'scale('+ scale + ', ' + scale + ')';
// 2.4 频率
var rate = Math.random() * 1.5;
span.style.animationDelay = rate + 's';
}
}
</script>
</body>
</html>
标签:opacity,特效,span,星海,100%,网页背景,background,var From: https://www.cnblogs.com/yidw/p/18586356