网页星空特效

模拟夜空的星星闪烁和流星划过

在线链接:夜空

效果图(看着不明显):

night-sky

代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>夜空</title>
    <style>
        #background {
            width: 100vw;
            height: 100vh;
            background: black;
            position: absolute;
            top: 0;
            left: 0;
        }

        .meteor {
            position: absolute;
            width: 3px;
            height: 3px;
            background: white;
            border-radius: 50%;
        }

        .star {
            position: absolute;
            width: 2px;
            height: 2px;
            background: white;
            border-radius: 50%;
        }

        @keyframes twinkle {
            0% {
                transform: scale(1);
            }

            50% {
                transform: scale(0.5);
                opacity: 0.2;
            }

            100% {
                transform: scale(1);
            }
        }

        @keyframes meteor {
            0% {
                top: -1px;
            }

            100% {
                top: 105%;
                transform: translate(-700px);
            }
        }
    </style>
</head>

<body>
    <!-- <div style="position: absolute;width: 100%;height: 100%;overflow: hidden;">
        <div id="background"></div>
    </div> -->
    <div id="background"></div>
    <script>
        function generateStar() {
            let star = document.createElement('div');
            star.classList.add('star');
            //随机持续时间1.5-2.5s
            star.style.animation = `twinkle ${Math.random() + 1.5}s linear infinite`;

            //随机横坐标
            star.style.left = Math.random() * window.innerWidth + 'px';

            //随机纵坐标
            star.style.top = Math.random() * window.innerHeight + 'px';

            document.getElementById('background').appendChild(star);
        }

        function removeStar() {
            let elements = document.getElementsByClassName('star');
            if (elements.length > 0) {
                elements[0].parentNode.removeChild(elements[0]);
            }
        }

        function generateMeteor() {

            let num = Math.random() * 4;
            //随机生成0~4个流星
            for (let i = 0; i < num; i++) {
                let meteor = document.createElement('div');
                meteor.classList.add('meteor');
                //随机持续时间1-3s
                meteor.style.animation = `meteor ${(Math.random() + 1) * 2}s linear infinite`;

                //随机横坐标700-最大
                meteor.style.left = 50 + Math.random() * (window.innerWidth - 50) + 'px';

                document.getElementById('background').appendChild(meteor);
            }

        }

        function removeMeteor() {
            let elements = document.getElementsByClassName('meteor');

            for (let i = elements.length - 1; i >= 0; i--) {
                let element = elements[i];
                const rect = element.getBoundingClientRect();
                let leftValue = rect.left;
                let topValue = rect.top;

                if (leftValue <= 10 || topValue >= (window.innerHeight - 30)) {
                    element.parentNode.removeChild(element);
                }
            }
        }

        //初始星星
        for (let i = 0; i < 99; i++) {
            generateStar();
        }

        //每隔一秒生成和消除一颗星星
        setInterval(() => {
            generateStar();
            removeStar();
        }, 1000)

        //初始流星
        for (let i = 0; i < 2; i++) {
            generateMeteor();

        }

        //间断生成流星
        setInterval(() => {
            generateMeteor();
        }, 3000)

        //间断消除流星
        setInterval(() => {
            removeMeteor();
        }, 100);
    </script>
</body>

</html>

很高兴能回答你的问题!网页制作星空特效CSS可以通过设置背景图片和CSS动画来实现。首先,您需要准备一张星空背景图片,并将其设置为页面的背景,然后使用CSS动画添加星星的闪烁效果。 以下是一些示范代码: HTML代码: ``` <body> <div class="stars"></div> <div class="twinkling"></div> <div class="clouds"></div> </body> ``` CSS代码: ``` body { background: #000 url(stars.jpg) repeat top center; overflow: hidden; } .stars { background: transparent url(stars.png) repeat top center; position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 0; } .twinkling { background: transparent url(twinkling.png) repeat top center; position: absolute; top: 0; left: 0; width: 100%; height: 100%; animation: move-twink-back 200s linear infinite; z-index: 1; } .clouds { background: transparent url(clouds.png) repeat top center; position: absolute; top: 0; left: 0; width: 100%; height: 100%; animation: move-clouds-back 200s linear infinite; z-index: 2; } @keyframes move-twink-back { 0% { background-position: 0 0; } 100% { background-position: 10000px 0; } } @keyframes move-clouds-back { 0% { background-position: 0 0; } 100% { background-position: 10000px 0; } } ``` 这个代码片段将星空背景图片设置为页面背景,然后使用CSS动画在星空中添加星星闪烁的效果,同时还添加了云层的移动效果。您可以添加更多的CSS动画来创建更多的效果,例如流星、星球等。希望这个答案能够帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愈辩已忘言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值