zoukankan      html  css  js  c++  java
  • js 烟花效果

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>烟花效果</title>
    <style type="text/css">
    *{margin:0;padding:0;}
    body{background:#000;overflow:hidden;}
    </style>
    </head>
    
    <body>
    <script type="text/javascript">
    window.onload = function(){
        var winWidth = document.documentElement.clientWidth || document.body.clientWidth;
        var winHeight = document.documentElement.clientHeight || document.body.clientHeight;
        var disX = 0;
        var disY = 0;
        var scroll = null;
        document.onclick = function(e){
            var oEvent = e || window.event;
            scroll = document.documentElement.scrollTop || document.body.scrollTop;
            disX = oEvent.clientX;
            disY = oEvent.clientY;
            var moveDiv = document.createElement("div");
            moveDiv.style.position = "absolute";
            moveDiv.style.left = disX + "px";
            moveDiv.style.top = winHeight + scroll + "px";
            moveDiv.style.width = "5px";
            moveDiv.style.height = "20px";
            moveDiv.style.background = "red";
            document.body.appendChild(moveDiv);
            moveDiv.timer = setInterval(function(){
                if(moveDiv.offsetTop<=disY){
                    clearInterval(moveDiv.timer);
                    document.body.removeChild(moveDiv);
                    bone();
                }else{
                    moveDiv.style.top = moveDiv.offsetTop - 20 + "px";    
                }
            },40)
        }    
        function bone(){
            var arr = [];
            var timer = null;
            var i;
            var len = 0;
            for(i=0;i<30;i++){
                var boneDiv = document.createElement("div");
                boneDiv.style.position = "absolute";
                boneDiv.style.left = disX + "px";
                boneDiv.style.top = disY + scroll + "px";
                boneDiv.style.width = "5px";
                boneDiv.style.height = "5px";
                boneDiv.style.background = "#"+getBg();
                document.body.appendChild(boneDiv);
                arr.push(boneDiv);
                boneDiv.iSpeedX = Math.ceil(Math.random()*20-10);
                boneDiv.iSpeedY = Math.ceil(Math.random()*20-10);
            }
            len = arr.length;
            timer = setInterval(function(){
                for(i=0,len=arr.length;i<len;i++){
                    if(! arr[i]){continue}
                    arr[i].style.left = arr[i].offsetLeft + arr[i].iSpeedX + "px";
                    arr[i].style.top = arr[i].offsetTop + arr[i].iSpeedY + "px";
                    arr[i].iSpeedY++;
                    if(arr[i].offsetLeft<=0 || arr[i].offsetLeft>=winWidth || arr[i].offsetTop <=0 || arr[i].offsetTop >= winHeight){
                        document.body.removeChild(arr[i])    
                        arr[i] = null;
                        len--;
                    }
                    if(len<=0){
                        clearInterval(timer);
                    }
                }
    
            },40)
        }
        function getBg(){
            var color = Math.ceil(Math.random()*16777215).toString(16);
            while(color<6){
                color = "#"+color;
            }
            return color;
        }
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    magic_quotes_gpc(魔术引号开关)
    获取文件绝对路径:__FILE__与 $_SERVER[SCRIPT_FILENAME''] 的 区别
    小程序wx:key中的关键字*this
    swiper 更改indicator-dots 属性 隐藏面板指示点
    B站视频下载
    makefile教程
    Qt 中配置 c99的问题
    C语言编译过程及相关文件
    go语言入门(10)并发编程
    go语言入门(9)文本文件处理
  • 原文地址:https://www.cnblogs.com/xiuciedward/p/3360985.html
Copyright © 2011-2022 走看看