html5 雪花特效


運行以下代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>snow</title>
    <style>
    *{margin: 0;padding: 0;}
    html{
        height:100%;
    }
    body{
        height:100%;
        /*background-color: rgba(0, 0, 0,1);body比較特殊*/
        background-color: black;
        overflow: hidden;          
    }
    /*.one{
   

    }*/
   
    </style>
</head>
<body>
    <!--<div class="one">
     
    </div>-->
    <script>
        // var h=document.body.offsetHeight;/*body的可視高度*/
        // alert(h);
        function Flake(){
                 //創建一張圖片
               var f=document.createElement('img');
                 //獲得文檔寬
                var w=document.documentElement.clientWidth;
                 //獲得文檔高
                 //實際上,上面html的高和寬都是繼承document的  
                var h=document.documentElement.clientHeight;
                //定義隨機出現的圖片
                var left=Math.random()*w;
                var top=Math.random()*h;
              //給圖片添加路徑在執行函數flake時讓他顯示出來,    Math.ceil(Math.random()*4是指有四個隨機圖片。
            f.src='images/'+Math.ceil(Math.random()*4)+'.jpg';
            f.style.position='absolute';
            //  f.style.z-index='1';
            f.style.left=left+'px';/*字符串拼接*/
            f.style.top=top+'px';
            f.style.transform='scale('+Math.random()+')';
             //必須把創建出來的img添加到body里才能顯示
            document.body.appendChild(f);
            function down(){
               left+=Math.random()*5;
               top+=Math.random()*3;
                if(left>w)   left=-20;
                if(top>h)   top=-20;
               
                 f.style.left=left+'px';
                 f.style.top=top+'px';
            }
            setInterval(down,20);
        }
     for(var i=0;i<20;i++){
        new Flake();
     }
    </script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM