CSS3中利用animation屬性創(chuàng)建雪花飄落特效
發(fā)布時(shí)間:2014-05-14 10:55:36 作者:佚名
我要評(píng)論

在CSS3中我們可以使用animation屬性來創(chuàng)建復(fù)雜的動(dòng)畫效果,本文就要借助它實(shí)現(xiàn)雪花飄落特效,功能代碼如下,希望對(duì)大家學(xué)習(xí)css3有所幫助
先介紹一下CSS3中的animation的特性吧。
在CSS3中我們可以使用animation屬性來創(chuàng)建復(fù)雜的動(dòng)畫效果,包括移動(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>
兩張雪花圖片:
最終效果:
在CSS3中我們可以使用animation屬性來創(chuàng)建復(fù)雜的動(dòng)畫效果,包括移動(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)。
比如:
復(fù)制代碼
代碼如下:@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)雪花飄落功能:
復(fù)制代碼
代碼如下:<!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>
兩張雪花圖片:

最終效果:

相關(guān)文章
css3的動(dòng)畫特效之動(dòng)畫序列(animation)
這篇文章主要介紹了css3的動(dòng)畫特效之動(dòng)畫序列(animation) 的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-22CSS3與動(dòng)畫有關(guān)的屬性transition、animation、transform對(duì)比(史上最全
這篇文章主要介紹了CSS3與動(dòng)畫有關(guān)的屬性transition、animation、transform對(duì)比,通過瀏覽器兼容性,用法和對(duì)比更深刻的展示了彼此之間的異同,具體操作步驟大家可查看下文2017-08-18CSS3 animation實(shí)現(xiàn)簡易幻燈片輪播特效
這篇文章主要為大家詳細(xì)介紹了CSS3 animation實(shí)現(xiàn)簡易幻燈片輪播特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-27CSS3中動(dòng)畫屬性transform、transition和animation屬性的區(qū)別
最近在項(xiàng)目中用到了CSS3中的動(dòng)畫屬性。無奈對(duì)于css3幾個(gè)新加的屬性不太熟悉,常常容易搞混。所以從網(wǎng)站研究了點(diǎn)資料,總結(jié)一下,方便有需要的朋友們可以參考學(xué)習(xí)。2016-09-25- 這篇文章主要為大家詳細(xì)介紹了CSS3中Animation動(dòng)畫屬性用法,教大家如何使用animation動(dòng)畫,感興趣的小伙伴們可以參考一下2016-07-04
CSS3 animation實(shí)現(xiàn)逐幀動(dòng)畫效果
這篇文章主要介紹了CSS3 animation實(shí)現(xiàn)逐幀動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-02CSS3中的Transition過度與Animation動(dòng)畫屬性使用要點(diǎn)
這篇文章主要介紹了CSS3中的Transition過度與Animation動(dòng)畫屬性使用要點(diǎn)Transition和Animation能被用來制作基本的頁面圖片動(dòng)態(tài)效果,當(dāng)然進(jìn)一步的控制還是需要JavaScript的2016-05-20利用css3-animation實(shí)現(xiàn)逐幀動(dòng)畫效果
這篇文章主要介紹了利用css3-animation實(shí)現(xiàn)逐幀動(dòng)畫效果的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-03-10- 這篇文章主要介紹了CSS3中Animation屬性的使用詳解,是CSS3入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-06
CSS3的transition和animation的用法實(shí)例介紹
transition用于設(shè)定一個(gè)元素的兩個(gè)狀態(tài),不同的狀態(tài)可以用偽類,下面與大家分享下CSS3的transition和animation的用法,需要的朋友可以參考下2014-08-20