zoukankan      html  css  js  c++  java
  • 3分钟实现星空图

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

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

    <style>
            *{
                margin: 0;
                padding: 0;
                list-style: none;
            }
    
            body{
                background-color: #000;
            }
    
            span{
                 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{
                 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>
  • 相关阅读:
    禁用Clusterware在系统启动后自己主动启动
    码农的产品思维培养第4节----听用户饿但不要照着做《人人都是产品经理》
    android RecycleView复杂多条目的布局
    【shell脚本练习】网卡信息和简单日志分析
    Java太阳系小游戏分析和源代码
    《你是我的眼》,歌曲非常好听
    hdu 1856 More is better(并查集)
    Python 中的isinstance函数
    Python中的 isdigit()方法
    Python中的split()函数的使用方法
  • 原文地址:https://www.cnblogs.com/pingfandezhuanji/p/12434582.html
Copyright © 2011-2022 走看看