標籤:
自訂動畫:
1.animation-name(自訂動畫名稱)
元素所應用的動畫名稱,必須與@keyframes使用,名稱由@keyframes定義。
keyframes(動畫主要畫面格):以@keyframes開頭,其中{}所寫入的是不同時間段的樣式規則。
<style type="text/css"> .div { width: 100px; height: 100px; background: blue; animation-name: lefttoright; } @keyframes lefttoright { /*定義動畫名稱為lefttoright*/
from {margin-left:0px;}
to {margin-left:300px;}
}
</style>
2.animation-duration(動畫時間)
<style type="text/css">.div { width:100px; height: 100px; background:blue; animation-name:lefttoright; animation-duration: 3s; }@keyframes lefttoright { from {margin-left: 0} to {margin-left: 300px;} }
3.animation-timing-function(動畫過渡速度類型)
linear:線性過渡
ease:平滑過渡
ease-in:由慢到快
ease-out:由快到慢
ease-in-out:由慢到快再到慢
<style type="text/css">.div { width: 100px; height: 100px; background: blue; animation-name: lefttoright; animation-timing-function:ease-out; /*由快到慢*/ } @keyframes lefttoright { from {margin-left: 0} to {margin-left: 300px;} } </style>
4.animation-delay:(動畫延遲時間)
<style type="text/css">.div { width: 100px; height: 100px; background: blue; animation-name: lefttoright; animation-timing-function:ease-out; animation-delay:2s; } @keyframes lefttoright { from {margin-left: 0} to {margin-left: 300px;} }</style>
5.animation-iteration-count(動畫執行次數)
<style type="text/css"> .div { width: 100px; height: 100px; background: blue; animation-name: lefttoright; animation-timing-function:ease-out; animation-delay:2s; animation-iteration-count:infinite; /*無數次*/ } @keyframes lefttoright { from {margin-left: 0} to {margin-left: 300px;} } </style>
6.animation-direction(動畫順序)
normal:正常方向
reverse:反方向運動
alternate:先正常運動再反方向運動,並持續交替運動
aiternate-reverse:先反方向運動再正方向,並持續交替運動
<style type="text/css">.div { width: 100px; height: 100px; background: blue; animation-name: lefttoright; animation-timing-function:ease-out; animation-delay:2s; animation-iteration-count:infinite; animation-direction:alternate; /*動畫先正常運行再反方向運行,並持續交替運行*/ } @keyframes lefttoright { from {margin-left: 0} to {margin-left: 300px;} } </style>
7. animation-play-state(動畫狀態)
<style type="text/css">.div { width:100px; height: 100px; background:blue; animation-name:lefttoright; animation-duration: 3s; animation-timing-function:ease-in; animation-delay:2s; animation-iteration-count:infinite; animation-direction:alternate; animation-fill-mode: forwards; } .div:hover { animation-play-state:paused; } /*當滑鼠移動到div中,動畫暫停*/ @keyframes lefttoright { from {margin-left: 0} to {margin-left: 300px;} }</style>
8.animation-fill-mode(動畫時間之外的狀態)
none:預設值,不設定對象動畫之外的狀態
forwards:設定對象為動畫結束時的狀態
backwards:設定對象為動畫開始時的狀態
both:設定對象為動畫開始或結束的狀態
<style type="text/css">.div { width: 100px; height: 100px; background: blue; animation-name: lefttoright; animation-timing-function:ease-out; animation-delay:2s; animation-iteration-count:infinite; animation-direction:alternate; animation-fill-mode: forwards; } @keyframes lefttoright { from {margin-left: 0} to {margin-left: 300px;} } </style>
9.animation(動畫複合屬性)
文法:animation:[ animation-name ] || [ animation-duration ] || [ animation-timing-function ] || [ animation-delay ] || [animation-iteration-count ] || [ animation-direction ] || <single-animation-fill-mode> || <single-animation-play-state>
<style type="text/css">.div{ width: 100px; height: 100px; background: blue; animation:lefttoright 3s ease-out forwards;} @keyframes lefttoright { from {margin-left: 0} to {margin-left: 300px;}}</style>
自訂動畫css屬性