html+css+css3+js实现动态星空

HTML:

结构页面创建一个id名的div盒子 CSS: body { background: radial-gradient(200% 10% at bottom center, #DE8887, #bebebe 40%, #242f4e); background: radial-gradient(220% 105% at bottom center, #DE8887, #bebebe 40%, #242f4e); background-attachment: fixed; overflow: hidden; top: 0; left: 0; right: 0; bottom: 0; }

@keyframes rotate {
0% {
transform: perspective(400px) rotateZ(200deg) rotateX(-40deg) rotateY(0);
}
100% {
transform: perspective(400px) rotateZ(200deg) rotateX(-40deg) rotateY(360deg);
}
}

#stars {
transform: perspective(500p);
transform-style: preserve-3d;
position: absolute;
perspective-origin: 50% 100%;
left: 50%;
right: 20%;
animation: rotate 90s infinite linear;
top: 0;
}

.star {
width: 4px;
height: 4px;
border-radius: 5px;
background-color: whitesmoke;
position: absolute;
top: 0;
left: 0;
backface-visibility: hidden;
}
CSS:给网页一个夜空的渐变背景,调节星空位置并打开3d维度,星星移动以动画的形式定义

JS:window.onload = function() {
var starcount = 900;
var distance = 800;
var stars = document.getElementById(“stars”);
for (var i = 0; i < starcount; i++) {
var speed = 0.1 + (Math.random() * 1);
var tdistance = distance + (Math.random() * 300)
stars.innerHTML += <div class="star" id="star${i}" style='transform-origin:0 0 ${tdistance}px;transform:translate3d(0,0,-${tdistance}px) rotateY(${(Math.random() * 360)}deg) rotateX(${(Math.random() * -50)}deg)' scale(${speed},${speed})> </div>;
};
}
生成定量的星星(900),并且每一颗星星的运动轨迹以随机数的形式赋值,Math.radom()生成随机数,
最终效果:在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的动态网页特效,用HTMLCSSJS实现。页面背景有动态星空效果,同时有一些祝福语和动态的加油口号。希望对你有帮助! HTML代码: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>高考加油!</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="stars"></div> <div class="container"> <h1>高考加油!</h1> <p>亲爱的高考考生们:</p> <p>今天是你们人生中的重要一天!</p> <p>你们经过了长达三年的努力,终于迎来了高考这个关键的时刻。</p> <p>无论成绩如何,你们都是优秀的,都值得尊重和骄傲。</p> <p>无论你们的未来道路如何,都希望你们能够保持乐观、坚强、自信的态度,勇敢地面对未来的挑战。</p> <p>最后,祝愿你们都能够取得优异的成绩,实现自己的梦想!</p> <p>高考加油!</p> <div class="slogan"> <h2>每个人都有自己的天空</h2> <h2>高考加油,飞向属于你的星空!</h2> </div> </div> <script src="script.js"></script> </body> </html> ``` CSS代码: ``` * { box-sizing: border-box; margin: 0; padding: 0; } body { background-color: black; color: white; font-family: Arial, sans-serif; text-align: center; } .stars { background-image: url(stars.png); background-repeat: repeat; position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: -1; } .container { margin-top: 50px; } h1 { font-size: 48px; margin-bottom: 20px; } p { font-size: 24px; margin-bottom: 10px; line-height: 1.5; } .slogan { margin-top: 50px; } .slogan h2 { font-size: 36px; margin-bottom: 10px; } .slogan h2:last-child { animation: blink 2s infinite; } @keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } } ``` JS代码: ``` // 生成随机星星 function generateStars() { var count = 100; var stars = document.querySelector('.stars'); for (var i = 0; i < count; i++) { var star = document.createElement('div'); var x = Math.floor(Math.random() * window.innerWidth); var y = Math.floor(Math.random() * window.innerHeight); var size = Math.floor(Math.random() * 3) + 1; var duration = Math.floor(Math.random() * 10) + 5; star.classList.add('star'); star.style.left = x + 'px'; star.style.top = y + 'px'; star.style.width = size + 'px'; star.style.height = size + 'px'; star.style.animationDuration = duration + 's'; stars.appendChild(star); } } generateStars(); ``` 在同一目录下,需要创建以下文件: - `stars.png`:星星背景图; - `style.css`:CSS文件; - `script.js`:JS文件。 由于无法在此处上传图片和文件,因此无法直接演示该动态网页特效。如果想要查看效果,可以新建一个HTML文件,将上述代码复制到文件中,并将相关文件放置在同一目录下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值