CSS3的transition和animation的用法實(shí)例介紹
發(fā)布時間:2014-08-20 10:11:24 作者:佚名
我要評論

transition用于設(shè)定一個元素的兩個狀態(tài),不同的狀態(tài)可以用偽類,下面與大家分享下CSS3的transition和animation的用法,需要的朋友可以參考下
transition
transition 屬性是
transition-property,
transition-duration,
transition-timing-function,
transition-delay
的簡稱,用于設(shè)定一個元素的兩個狀態(tài),不同的狀態(tài)可以用偽類,比如:hover, :active 或者是通過 javascript 動態(tài)設(shè)定。IE10+支持。
所以 transition 的初始值為:
transition-delay: 0s;
transition-duration: 0s;
transition-property: all;
transition-timing-function: ease;
用法
div {
transition: <property> <duration> <timing-function> <delay>;
}
并且有事件可以監(jiān)測 transition 結(jié)束
el.addEventListener("transitionend",updateTransition,true);
//in webkit
el.addEventListener("webkitTransitionEnd",updateTransition,true);
實(shí)例
HTML
<!-- DEMO 1: Fade Block -->
<div id="fade">
move here !
</div>
<div id="nudge">
mouse on me
</div>
<div id="bounce">Place mouse on me i will bounce!</div>
<div id="spin">Place mouse on me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i will spin</div>
<div id="accordion" class="accordion">
<a href="#first">This is first tab</a>
<div id="first"><p>Lorem ipsum </p> </div>
<a href="#second">This is second tab</a>
<div id="second"><p>Lorem ipsum </p> </div>
<a href="#third">This is third tab</a>
<div id="third"><p>Lorem ipsum </p> </div>
</div>
CSS
/*
DEMO 1: Fade Block
*/
div {
margin-bottom: 50px;
}
#fade {
/*opacity:1;
-webkit-transition: opacity 10s liner 10s;*/
position: relative;
transition-property: font-size;
transition-duration: 0.5s;
transition-delay: 0;
font-size: 14px;
}
#fade:hover {
font-size: 36px;
}
/* DEMO2 */
#nudge{
-webkit-transition-property: color,
background-color,padding-left;
-webkit-transition-duration: 500ms,500ms, 500ms;
}
#nudge:hover{
background-color: #efefef;
color: #333;
padding-left: 50px;
}
#bounce:hover {
-webkit-animation-name:bounce;
-webkit-animation-duration:1s;
-webkit-animation-iteration-count:2;
-webkit-animation-direction:alternate
}
@-webkit-keyframes bounce {
from{margin-left:0;}
to{margin-left:250px;}
}
#spin{
-webkit-transition: -webkit-transform 10s ease-in;
}
#spin:hover{
-webkit-transform: rotate(36000deg);
}
.accordion a{
display: block;
padding:5px 10px;
background-color:#ccc;
color:#000;
/*可以去掉鏈接的下劃線等修飾效果*/
text-decoration:none;
}
.accordion a:hover{
background-color:#999;
}
.accordion div{
background-color:#cda;
color:#222;
}
.accordion div p{
padding:20px
}
#accordion div{
/*先隱藏起來*/
height:0;
overflow:hidden;
-webkit-transition:height 600ms ease;
}
#accordion div:target{
height:110px;
}
animation
animation 屬性是如下這些屬性的簡寫
animation-name: none
animation-duration: 0s
animation-timing-function: ease
animation-delay: 0s
animation-iteration-count: 1
animation-direction: normal
animation-fill-mode: none
用法
animation:
animation-name
time(duration)
timing-function
time(delay)
animation-iteration-count( 結(jié)束之前的循環(huán)次數(shù))
single-animation-direction
/*{
animation-direction: normal (每次從正方向開始)
animation-direction: reverse (每次從反方向開始)
animation-direction: alternate (正反往復(fù))
}*/
single-animation-fill-mode
實(shí)例
<div class="view_port">
<div class="polling_message">
Listener for dispatches
</div>
<div class="cylon_eye">
</div>
</div>
.polling_message {
color: white;
float: left;
margin-right:2%;
}
.view_port {
background-color: black;
height: 50px;
width: 100%;
overflow: hidden;
}
.cylon_eye {
color: white;
height: 100%;
width: 80%;
background-color: red;
background-image: linear-gradient(to right, rgba(0, 0, 0, 0.9) 25%, rgba(0, 0, 0, 0.1) 50%, rgba(0, 0, 0, 0.9) 75%);
-webkit-animation: move_eye 4s linear 0s infinite alternate;
-moz-animation: move_eye 4s linear 0s infinite alternate;
-o-animation: move_eye 4s linear 0s infinite alternate;
animation: move_eye 4s linear 0s infinite alternate;
}
@-webkit-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@-moz-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@-o-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
transition 屬性是
transition-property,
transition-duration,
transition-timing-function,
transition-delay
的簡稱,用于設(shè)定一個元素的兩個狀態(tài),不同的狀態(tài)可以用偽類,比如:hover, :active 或者是通過 javascript 動態(tài)設(shè)定。IE10+支持。
所以 transition 的初始值為:
transition-delay: 0s;
transition-duration: 0s;
transition-property: all;
transition-timing-function: ease;
用法
div {
transition: <property> <duration> <timing-function> <delay>;
}
并且有事件可以監(jiān)測 transition 結(jié)束
el.addEventListener("transitionend",updateTransition,true);
//in webkit
el.addEventListener("webkitTransitionEnd",updateTransition,true);
實(shí)例
HTML
復(fù)制代碼
代碼如下:<!-- DEMO 1: Fade Block -->
<div id="fade">
move here !
</div>
<div id="nudge">
mouse on me
</div>
<div id="bounce">Place mouse on me i will bounce!</div>
<div id="spin">Place mouse on me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i won me i will spin</div>
<div id="accordion" class="accordion">
<a href="#first">This is first tab</a>
<div id="first"><p>Lorem ipsum </p> </div>
<a href="#second">This is second tab</a>
<div id="second"><p>Lorem ipsum </p> </div>
<a href="#third">This is third tab</a>
<div id="third"><p>Lorem ipsum </p> </div>
</div>
CSS
復(fù)制代碼
代碼如下:/*
DEMO 1: Fade Block
*/
div {
margin-bottom: 50px;
}
#fade {
/*opacity:1;
-webkit-transition: opacity 10s liner 10s;*/
position: relative;
transition-property: font-size;
transition-duration: 0.5s;
transition-delay: 0;
font-size: 14px;
}
#fade:hover {
font-size: 36px;
}
/* DEMO2 */
#nudge{
-webkit-transition-property: color,
background-color,padding-left;
-webkit-transition-duration: 500ms,500ms, 500ms;
}
#nudge:hover{
background-color: #efefef;
color: #333;
padding-left: 50px;
}
#bounce:hover {
-webkit-animation-name:bounce;
-webkit-animation-duration:1s;
-webkit-animation-iteration-count:2;
-webkit-animation-direction:alternate
}
@-webkit-keyframes bounce {
from{margin-left:0;}
to{margin-left:250px;}
}
#spin{
-webkit-transition: -webkit-transform 10s ease-in;
}
#spin:hover{
-webkit-transform: rotate(36000deg);
}
.accordion a{
display: block;
padding:5px 10px;
background-color:#ccc;
color:#000;
/*可以去掉鏈接的下劃線等修飾效果*/
text-decoration:none;
}
.accordion a:hover{
background-color:#999;
}
.accordion div{
background-color:#cda;
color:#222;
}
.accordion div p{
padding:20px
}
#accordion div{
/*先隱藏起來*/
height:0;
overflow:hidden;
-webkit-transition:height 600ms ease;
}
#accordion div:target{
height:110px;
}
animation
animation 屬性是如下這些屬性的簡寫
animation-name: none
animation-duration: 0s
animation-timing-function: ease
animation-delay: 0s
animation-iteration-count: 1
animation-direction: normal
animation-fill-mode: none
用法
animation:
animation-name
time(duration)
timing-function
time(delay)
animation-iteration-count( 結(jié)束之前的循環(huán)次數(shù))
single-animation-direction
/*{
animation-direction: normal (每次從正方向開始)
animation-direction: reverse (每次從反方向開始)
animation-direction: alternate (正反往復(fù))
}*/
single-animation-fill-mode
實(shí)例
復(fù)制代碼
代碼如下:<div class="view_port">
<div class="polling_message">
Listener for dispatches
</div>
<div class="cylon_eye">
</div>
</div>
.polling_message {
color: white;
float: left;
margin-right:2%;
}
.view_port {
background-color: black;
height: 50px;
width: 100%;
overflow: hidden;
}
.cylon_eye {
color: white;
height: 100%;
width: 80%;
background-color: red;
background-image: linear-gradient(to right, rgba(0, 0, 0, 0.9) 25%, rgba(0, 0, 0, 0.1) 50%, rgba(0, 0, 0, 0.9) 75%);
-webkit-animation: move_eye 4s linear 0s infinite alternate;
-moz-animation: move_eye 4s linear 0s infinite alternate;
-o-animation: move_eye 4s linear 0s infinite alternate;
animation: move_eye 4s linear 0s infinite alternate;
}
@-webkit-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@-moz-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@-o-keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
@keyframes move_eye {
from {
margin-left:-20%;
}
to {
margin-left:100%;
}
}
相關(guān)文章
- 這篇文章主要介紹了css3的動畫特效之動畫序列(animation) 的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-22
CSS3與動畫有關(guān)的屬性transition、animation、transform對比(史上最全
這篇文章主要介紹了CSS3與動畫有關(guān)的屬性transition、animation、transform對比,通過瀏覽器兼容性,用法和對比更深刻的展示了彼此之間的異同,具體操作步驟大家可查看下文2017-08-18CSS3 animation實(shí)現(xiàn)簡易幻燈片輪播特效
這篇文章主要為大家詳細(xì)介紹了CSS3 animation實(shí)現(xiàn)簡易幻燈片輪播特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-27CSS3中動畫屬性transform、transition和animation屬性的區(qū)別
最近在項(xiàng)目中用到了CSS3中的動畫屬性。無奈對于css3幾個新加的屬性不太熟悉,常常容易搞混。所以從網(wǎng)站研究了點(diǎn)資料,總結(jié)一下,方便有需要的朋友們可以參考學(xué)習(xí)。2016-09-25- 這篇文章主要為大家詳細(xì)介紹了CSS3中Animation動畫屬性用法,教大家如何使用animation動畫,感興趣的小伙伴們可以參考一下2016-07-04
CSS3 animation實(shí)現(xiàn)逐幀動畫效果
這篇文章主要介紹了CSS3 animation實(shí)現(xiàn)逐幀動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-02CSS3中的Transition過度與Animation動畫屬性使用要點(diǎn)
這篇文章主要介紹了CSS3中的Transition過度與Animation動畫屬性使用要點(diǎn)Transition和Animation能被用來制作基本的頁面圖片動態(tài)效果,當(dāng)然進(jìn)一步的控制還是需要JavaScript的2016-05-20利用css3-animation實(shí)現(xiàn)逐幀動畫效果
這篇文章主要介紹了利用css3-animation實(shí)現(xiàn)逐幀動畫效果的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-03-10- 這篇文章主要介紹了CSS3中Animation屬性的使用詳解,是CSS3入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-08-06
CSS3中利用animation屬性創(chuàng)建雪花飄落特效
在CSS3中我們可以使用animation屬性來創(chuàng)建復(fù)雜的動畫效果,本文就要借助它實(shí)現(xiàn)雪花飄落特效,功能代碼如下,希望對大家學(xué)習(xí)css3有所幫助2014-05-14