zoukankan      html  css  js  c++  java
  • 用js实现雪花下落的功能

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>雪花下落</title>
    </head>
    <style>
        .box{
            height: 600px;
             800px;
            border: 1px solid red;
            position: relative;
            overflow: hidden;
        }
        .box div{
            position:absolute;
        }
    </style>
    <body>
        <div class="box" id="box">
        </div>
        <input type="button" value="生成雪花" id="btn" />
    </body>
    <script>
        var box=document.getElementById("box");
        var btn=document.getElementById("btn");
        //生成雪花
        function createSnow(l,t,s){
            var newNode=document.createElement("div");
             newNode.style.height=s+"px";
             newNode.style.width=s+"px";
             newNode.style.left=l+"px";
             newNode.style.top=t+"px";
             newNode.style.background="url('img/snow.png')";
             newNode.style.backgroundSize="100% 100%";
             box.appendChild(newNode);
             startMove(newNode);
        }
        var time1=null;
        var poY=0;
        btn.onclick=function(){

            setInterval(function(){
                var le=parseInt(Math.random()*770);//随机生成位置及宽高
                var to=parseInt(Math.random()*570);
                var size=parseInt(Math.random()*10+20);
                createSnow(le,to,size);
            },100);
            
        }
        //雪花下落
        function startMove(obj){
            var poY=obj.offsetTop;//获取对象到顶部边宽的距离
            var time2=null;
            function move(){
                poY++;
                obj.style.top=poY+"px";
                if(poY>600){//当雪花超出容器时的处理事件
                    clearInterval(time2);//清除计时器
                    box.removeChild(obj);//删除超出容器的节点对象
                }
            }
            time2=setInterval(move,10);
        }
    </script>
    </html>

  • 相关阅读:
    Linux配置dhcp自动获取ip地址
    CentOS7密码复杂度配置
    JDK切换和BurpSuite破解
    python操作Elasticsearch7.x
    mongoShake docker 部署 (mongoshake:2.6.5)
    elasticsearch 7.x 安装使用(ik,elasticsearch head )
    mongoDB -- 全文索引
    商业模式画布
    Vue全家桶--12 Vue-CLI 3.x 脚手架构建项目
    Vue全家桶--11 Webpack和Vue-Loader打包资源
  • 原文地址:https://www.cnblogs.com/html-go/p/5823581.html
Copyright © 2011-2022 走看看