css3中利用animation屬性創(chuàng)建雪花飄落特效

字號(hào):


    先介紹一下css3中的animation的特性吧。
    在css3中我們可以使用animation屬性來(lái)創(chuàng)建復(fù)雜的動(dòng)畫(huà)效果,包括移動(dòng),旋轉(zhuǎn),縮放,傾斜(后幾個(gè)請(qǐng)參考css3中的transform,scale等屬性)等。而這一切,只需要我們創(chuàng)建關(guān)鍵幀(@keyframes),然后將自己想要實(shí)現(xiàn)的動(dòng)作添加進(jìn)去就可以實(shí)現(xiàn)。
    比如:
    代碼如下:
    @keyframes bgchange{
    from {background:red;}
    to {background:yellow}
    }
    div:hover{
    animation:bgchange 5s;
    }
    當(dāng)鼠標(biāo)懸停在<div>時(shí),該<div>的背景顏色會(huì)在五秒之內(nèi)從紅色變?yōu)辄S色。
    注意:使用animation和@keyframes時(shí)不同瀏覽器需要加上不同的前綴名!
    下面代碼實(shí)現(xiàn)雪花飄落功能:
    代碼如下:
    <!doctype html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>snowing snow</title>
    <style>
    body{
    background: #eee;
    }
    @keyframes mysnow{
    0%{
    bottom:100%;
    opacity:0;
    }
    50%{
    opacity:1;
    transform: rotate(1080deg);
    }
    100%{
    transform: rotate(0deg);
    opacity: 0;
    bottom:0;
    }
    }
    @-webkit-keyframes mysnow{
    0%{
    bottom:100%;
    opacity:0;
    }
    50%{
    opacity:1;
    -webkit-transform: rotate(1080deg);
    }
    100%{
    -webkit-transform: rotate(0deg);
    opacity: 0;
    bottom:0;
    }
    }
    @-moz-keyframes mysnow{
    0%{
    bottom:100%;
    opacity:0;
    }
    50%{
    opacity:1;
    -moz-transform: rotate(1080deg);
    }
    100%{
    -moz-transform: rotate(0deg);
    opacity: 0;
    bottom:0;
    }
    }
    @-ms-keyframes mysnow{
    0%{
    bottom:100%;
    opacity:0;
    }
    50%{
    opacity:1;
    -ms-transform: rotate(1080deg);
    }
    100%{
    -ms-transform: rotate(0deg);
    opacity: 0;
    bottom:0;
    }
    }
    @-o-keyframes mysnow{
    0%{
    bottom:100%;
    opacity:0;
    }
    50%{
    opacity:1;
    -o-transform: rotate(1080deg);
    }
    100%{
    -o-transform: rotate(0deg);
    opacity: 0;
    bottom:0;
    }
    }
    .roll{
    position:absolute;
    opacity:0;
    animation: mysnow 5s ;
    -webkit-animation: mysnow 5s ;
    -moz-animation: mysnow 5s ;
    -ms-animation: mysnow 5s ;
    -o-animation: mysnow 5s ;
    height:80px;
    }
    .div{
    position:fixed;
    }
    </style>
    </head>
    <body>
    <div id=snowzone >
    </div>
    </body>
    <script>
    (function(){
    function snow(left,height,src){
    var div = document.createelement(div);
    var img = document.createelement(img);
    div.appendchild(img);
    img.classname = roll;
    img.src = src;
    div.style.left=left+px;
    div.style.height=height+px;
    div.classname=div;
    document.getelementbyid(snowzone).appendchild(div);
    settimeout(function(){
    document.getelementbyid(snowzone).removechild(div);
    // console.log(window.innerheight);
    },5000);
    }
    setinterval(function(){
    var left = math.random()*window.innerwidth;
    var height = math.random()*window.innerheight;
    var src = s+math.floor(math.random()*2+1)+.png;//兩張圖片分別為s1.png、s2.png
    snow(left,height,src);
    },500);
    })();
    </script>
    </html>