CSS(Cascading Style Sheets)樣式更改——過渡、動(dòng)畫
前言
這篇文章我們來(lái)介紹下CSS樣式更改中的過渡、動(dòng)畫基礎(chǔ)用法。
1.過渡
元素從一種樣式逐漸改變?yōu)榱硪环N的樣式 。
- div
- {
- transition: width 1s;
- -moz-transition: width 1s; /* Firefox 4 */
- -webkit-transition: width 1s; /* Safari 和 Chrome */
- -o-transition: width 1s; /* Opera */
- }
- transition-property:應(yīng)用過渡的Css屬性的名稱 比如寬度width
- transition-duration:過渡效果花費(fèi)的時(shí)間 比如1s
- transition-timing-function:渡效果的時(shí)間曲線 如下所示:
- linear 勻速
- ease 先慢后快
- ease-in 慢速開始
- ease-out 慢速結(jié)束
- ease-in-out 慢速開始和結(jié)束
- cubic-bezier(n,n,n,n) 在cubic-bezie 函數(shù)中定義自己的值,可能的值是0至1之間的數(shù)值
- transition-delay:過渡效果何時(shí)開始 如1s
2.動(dòng)畫 Animation
1).首先定義@keyframes 規(guī)則
- @keyframes my
- {
- from {background: red;}
- to {background: yellow;}
- }
- @-moz-keyframes my /* Firefox */
- {
- from {background: red;}
- to {background: yellow;}
- }
- @-webkit-keyframes my /* Safari 和 Chrome */
- {
- from {background: red;}
- to {background: yellow;}
- }
- @-o-keyframes my /* Opera */
- {
- from {background: red;}
- to {background: yellow;}
- }
為了豐富元素的變化過程,你可以把from to改為百分比的樣子:
- @keyframes my
- {
- 0% {background: red;}
- 25% {background: yellow;}
- 50% {background: blue;}
- 100% {background: green;}
- }
- @-moz-keyframes my /* Firefox */
- {
- 0% {background: red;}
- 25% {background: yellow;}
- 50% {background: blue;}
- 100% {background: green;}
- }
- @-webkit-keyframes my /* Safari 和 Chrome */
- {
- 0% {background: red;}
- 25% {background: yellow;}
- 50% {background: blue;}
- 100% {background: green;}
- }
- @-o-keyframes my /* Opera */
- {
- 0% {background: red;}
- 25% {background: yellow;}
- 50% {background: blue;}
- 100% {background: green;}
- }
定義好了,接下來(lái)我們就可以啟動(dòng)我們的動(dòng)畫了。
2).animation啟動(dòng)動(dòng)畫效果
- div
- {
- animation-name: my;
- 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: my;
- -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: my;
- -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: my;
- -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-name 選擇器的 keyframes 的名稱
- animation-duration 動(dòng)畫所花費(fèi)的時(shí)間
- animation-timing-function 勻速播放動(dòng)畫
- animation-delay 動(dòng)畫過多久開始
- animation-iteration-count 播放動(dòng)畫次數(shù)
- animation-direction 是否在下一周期逆向地播放 normal 正常播放 alternate 輪流反向播放
- animation-play-state 暫停動(dòng)畫 paused 動(dòng)畫已暫停 running 動(dòng)畫正在播放
- animation-fill-mode
- none 不填充
- forwards 當(dāng)動(dòng)畫完成后,保持最后一個(gè)屬性值
- backwards 在animation-delay 所指定的一段時(shí)間內(nèi),在動(dòng)畫顯示之前,應(yīng)用開始屬性值
- both 向前和向后填充模式都被應(yīng)用。
參考文檔:W3C官方文檔(CSS篇)
總結(jié)
這篇文章主要介紹了CSS樣式更改篇中的過度和動(dòng)漫基礎(chǔ)知識(shí),希望讓大家對(duì)CSS樣式更改有個(gè)簡(jiǎn)單的認(rèn)識(shí)和了解。