【Java Saves!】Session 2:意圖。本站提示廣大學習愛好者:(【Java Saves!】Session 2:意圖)文章只能為提供參考,不一定能成為您想要的結果。以下是【Java Saves!】Session 2:意圖正文
經過 CSS3,我們可以創立動畫,這可以在許多網頁中取代動畫圖片、Flash 動畫以及 JavaScript。
CSS3 動畫
如需在 CSS3 中創立動畫,您需求學習 @keyframes 規則。
@keyframes 規則用於創立動畫。在 @keyframes 中規則某項 CSS 款式,就能創立由以後款式逐步改為新款式的動畫效果。
Internet Explorer 10、Firefox 以及 Opera 支持 @keyframes 規則和 animation 屬性。
Chrome 和 Safari 需求前綴 -webkit-。
正文:Internet Explorer 9,以及更早的版本,不支持 @keyframe 規則或 animation 屬性。
@keyframes myfirst
{
from {background: red;}
to {background: yellow;}
}
@-moz-keyframes myfirst /* Firefox */
{
from {background: red;}
to {background: yellow;}
}
@-webkit-keyframes myfirst /* Safari 和 Chrome */
{
from {background: red;}
to {background: yellow;}
}
@-o-keyframes myfirst /* Opera */
{
from {background: red;}
to {background: yellow;}
}
當您在 @keyframes 中創立動畫時,請把它捆綁到某個選擇器,否則不會發生動畫效果。
經過規則至多以下兩項 CSS3 動畫屬性,即可將動畫綁定到選擇器:
把 "myfirst" 動畫捆綁到 div 元素,時長:5 秒:
div
{
animation: myfirst 5s;
-moz-animation: myfirst 5s; /* Firefox */
-webkit-animation: myfirst 5s; /* Safari 和 Chrome */
-o-animation: myfirst 5s; /* Opera */
}
正文:您必需定義動畫的稱號和時長。假如疏忽時長,則動畫不會允許,由於默許值是 0。
動畫是使元素從一種款式逐步變化為另一種款式的效果。
您可以改動恣意多的款式恣意多的次數。
請用百分比來規則變化發作的時間,或用關鍵詞 "from" 和 "to",同等於 0% 和 100%。
0% 是動畫的開端,100% 是動畫的完成。
為了失掉最佳的閱讀器支持,您應該一直定義 0% 和 100% 選擇器。
當動畫為 25% 及 50% 時改動背風光,然後當動畫 100% 完成時再次改動:
@keyframes myfirst
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
@-moz-keyframes myfirst /* Firefox */
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
@-webkit-keyframes myfirst /* Safari 和 Chrome */
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
@-o-keyframes myfirst /* Opera */
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
改動背風光和地位:
@keyframes myfirst
{
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
@-moz-keyframes myfirst /* Firefox */
{
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
@-webkit-keyframes myfirst /* Safari 和 Chrome */
{
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
@-o-keyframes myfirst /* Opera */
{
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
上面的表格列出了 @keyframes 規則和一切動畫屬性:
上面的兩個例子設置了一切動畫屬性:
運轉名為 myfirst 的動畫,其中設置了一切動畫屬性:
div
{
animation-name: myfirst;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-play-state: running;
/* Firefox: */
-moz-animation-name: myfirst;
-moz-animation-duration: 5s;
-moz-animation-timing-function: linear;
-moz-animation-delay: 2s;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
-moz-animation-play-state: running;
/* Safari 和 Chrome: */
-webkit-animation-name: myfirst;
-webkit-animation-duration: 5s;
-webkit-animation-timing-function: linear;
-webkit-animation-delay: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
-webkit-animation-play-state: running;
/* Opera: */
-o-animation-name: myfirst;
-o-animation-duration: 5s;
-o-animation-timing-function: linear;
-o-animation-delay: 2s;
-o-animation-iteration-count: infinite;
-o-animation-direction: alternate;
-o-animation-play-state: running;
}
與下面的動畫相反,但是運用了簡寫的動畫 animation 屬性:
div
{
animation: myfirst 5s linear 2s infinite alternate;
/* Firefox: */
-moz-animation: myfirst 5s linear 2s infinite alternate;
/* Safari 和 Chrome: */
-webkit-animation: myfirst 5s linear 2s infinite alternate;
/* Opera: */
-o-animation: myfirst 5s linear 2s infinite alternate;
}