經過 @keyframes 規則,您可以建立動畫。 css
建立動畫的原理是,將一套 CSS 樣式逐漸變化爲另外一套樣式。 html
在動畫過程當中,您可以屢次改變這套 CSS 樣式。 css3
以百分比來規定改變發生的時間,或者經過關鍵詞 "from" 和 "to",等價於 0% 和 100%。 web
0% 是動畫的開始時間,100% 動畫的結束時間。 瀏覽器
爲了得到最佳的瀏覽器支持,您應該始終定義 0% 和 100% 選擇器。 動畫
註釋:請使用動畫屬性來控制動畫的外觀,同時將動畫與選擇器綁定。 spa
@keyframes animationname {keyframes-selector {css-styles;}}
<!DOCTYPE html> <html> <head> <style> div { width:100px; height:100px; background:red; position:relative; animation:mymove 5s infinite; -moz-animation:mymove 5s infinite; /* Firefox */ -webkit-animation:mymove 5s infinite; /* Safari and Chrome */ -o-animation:mymove 5s infinite; /* Opera */ } @keyframes mymove { 0% {top:0px; background:red; width:100px;} 100% {top:200px; background:yellow; width:300px;} } @-moz-keyframes mymove /* Firefox */ { 0% {top:0px; background:red; width:100px;} 100% {top:200px; background:yellow; width:300px;} } @-webkit-keyframes mymove /* Safari and Chrome */ { 0% {top:0px; background:red; width:100px;} 100% {top:200px; background:yellow; width:300px;} } @-o-keyframes mymove /* Opera */ { 0% {top:0px; background:red; width:100px;} 100% {top:200px; background:yellow; width:300px;} } </style> </head> <body> <p><b>註釋:</b>本例在 Internet Explorer 中無效。</p> <div></div> </body> </html>CSS3的animation相似於transition屬性,他們都是隨着時間改變元素的屬性值。他們主要區別是transition須要觸發一個事件 (hover事件或click事件等)纔會隨時間改變其css屬性;而animation在不須要觸發任何事件的狀況下也能夠顯式的隨着時間變化來改變元 素css的屬性值,從而達到一種動畫的效果。這樣咱們就能夠直接在一個元素中調用animation的動畫屬性,基於這一點,css3的 animation就須要明確的動畫屬性值,咱們須要keyframes來定義不一樣時間的css屬性值,達到元素在不一樣時間 段變化的效果。
給一個元素調用animation屬性 3d
.demo1 { width: 50px; height: 50px; margin-left: 100px; background: blue; -webkit-animation-name:'wobble';/*動畫屬性名,也就是咱們前面keyframes定義的動畫名*/ -webkit-animation-duration: 10s;/*動畫持續時間*/ -webkit-animation-timing-function: ease-in-out; /*動畫頻率,和transition-timing-function是同樣的*/ -webkit-animation-delay: 2s;/*動畫延遲時間*/ -webkit-animation-iteration-count: 10;/*定義循環資料,infinite爲無限次*/ -webkit-animation-direction: alternate;/*定義動畫方式*/ }
=========================== code
自用: orm
/*定義旋轉*/
@-webkit-keyframes rock{ 0%{ transform:rotate(0deg) } 10%{ transform:rotate(3deg) } 20%{ transform:rotate(-3deg) } 30%{ transform:rotate(2deg) } 40%{ transform:rotate(-2deg) } 50%{ transform:rotate(1deg) } 60%{ transform:rotate(-1deg) } 70%{ transform:rotate(0deg) } 100%{ transform:rotate(0deg) } }
/**給須要抖動選中的元素加上動畫*/
.c_zongzi_box_rock{ -webkit-animation:rock 2s infinite; }
infinite是無限的動畫的意思 rock定義的-webkit-keyframes的名字,2s是動畫的時間
transform-origin是動畫變換的基點(參照點)
一、top left | left top 等價於 0 0 | 0% 0%
二、top | top center | center top 等價於 50% 0
三、right top | top right 等價於 100% 0
四、left | left center | center left 等價於 0 50% | 0% 50%
五、center | center center 等價於 50% 50%(默認值)
六、right | right center | center right 等價於 100% 50%
七、bottom left | left bottom 等價於 0 100% | 0% 100%
八、bottom | bottom center | center bottom 等價於 50% 100%
九、bottom right | right bottom 等價於 100% 100%
其中 left,center right是水平方向取值,對應的百分值爲left=0%;center=50%;right=100%而top center bottom是垂直方向的取值,其中top=0%;center=50%;bottom=100%;若是隻取一個值,表示垂直方向值不變