3分钟实现星空图

不需要多么高大上的编辑器,一个文本文档足够,首先在桌面新建一个文件夹,然后把素材图片放在文件夹,再新建一个文本文档。

先写入模板,对页面布局进行调整,代码如下:

<style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }

        body{
            background-color: #000;
        }

        span{
            width: 30px;
            height: 30px;
            background: url("图片路径") 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>

这个时候基本雏形就有了。我们把大小调整为电脑屏幕一样大小

var screenW = document.documentElement.clientWidth;
var screenH = document.documentElement.clientHeight;

然后产生随机,随机的跳动频率,随机的位置,随机的星星个数等

for(var i=0; i<150; i++){
            var span = document.createElement('span');
            document.body.appendChild(span);

var x = parseInt(Math.random() * screenW);
            var y = parseInt(Math.random() * screenH);
            span.style.left = x + 'px';
            span.style.top = y + 'px';

var scale = Math.random() * 1.5;
span.style.transform = 'scale('+ scale + ', ' + scale + ')';

var rate = Math.random() * 1.5;
span.style.animationDelay = rate + 's';

这个时候差不多就结束了,可以看看效果图

 下面附上完整代码

<!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("图片路径") 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>
<span></span>
<script>
    window.onload = function () {
        // 屏幕的尺寸
        var screenW = document.documentElement.clientWidth;
        var screenH = document.documentElement.clientHeight;

        // 2. 动态创建多个星星
        for(var i=0; i<150; i++){
            var span = document.createElement('span');
            document.body.appendChild(span);

            //位置随机
            var x = parseInt(Math.random() * screenW);
            var y = parseInt(Math.random() * screenH);
            span.style.left = x + 'px';
            span.style.top = y + 'px';

            //大小随机
            var scale = Math.random() * 1.5;
            span.style.transform = 'scale('+ scale + ', ' + scale + ')';

            //频率随机
            var rate = Math.random() * 1.5;
            span.style.animationDelay = rate + 's';
        }
    }
</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/pingfandezhuanji/p/12434582.html