CSS3 animation-timing-function

admin發表於2018-09-06

animation-timing-function屬性用於設定animation動畫的過渡型別。

更多關於animation動畫可以參閱CSS3 animation一章節。

語法結構:

[CSS] 純文字檢視 複製程式碼
animation-timing-function:linear | ease | ease-in | ease-out | ease-in-out | cubic-bezier(<number>, <number>, <number>, <number>) [ , linear | ease | ease-in | ease-out | ease-in-out | cubic-bezier(<number>, <number>, <number>, <number>) ]*

引數解析:

(1).linear:線性過渡。等同於貝塞爾曲線(0.0, 0.0, 1.0, 1.0) 

(2).ease:平滑過渡。等同於貝塞爾曲線(0.25, 0.1, 0.25, 1.0) 

(3).ease-in:由慢到快。等同於貝塞爾曲線(0.42, 0, 1.0, 1.0) 

(4).ease-out:由快到慢。等同於貝塞爾曲線(0, 0, 0.58, 1.0) 

(5).ease-in-out:由慢到快再到慢。等同於貝塞爾曲線(0.42, 0, 0.58, 1.0) 

(6).cubic-bezier(<number>, <number>, <number>, <number>): 特定的貝塞爾曲線型別,4個數值需在[0, 1]區間內;賽貝爾曲線型別可以線上生成,如貝塞爾曲線生成工具

特別說明:如果提供多個屬性值,以逗號進行分隔。 

程式碼例項:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!DOCTYPE html>    
<html>    
<head>    
<meta charset=" utf-8">    
<meta name="author" content="http://www.softwhy.com/" />    
<title>螞蟻部落</title>   
<style type="text/css">   
div{  
  width:100px;  
  height:100px;  
  background:red;  
  position:relative;  
       
  animation:theanimation 5s infinite alternate;  
  -webkit-animation:theanimation 5s infinite alternate;  
  -moz-animation:theanimation 5s infinite alternate;  
  -o-animation:theanimation 5s infinite alternate;  
     
  animation-timing-function:ease-in; 
  -webkit-animation-timing-function:ease-in; 
  -moz-animation-timing-function:ease-in;  
  -o-animation-timing-function:ease-in;   
}  
@keyframes theanimation{  
  0% {left:0px;}  
  100% {left:200px;}  
}  
@-webkit-keyframes theanimation{  
  0% {left:0px;}  
  100% {left:200px;}  
}  
@-moz-keyframes theanimation{  
  0% {left:0px;}  
  100% {left:200px;}  
}  
@-o-keyframes theanimation{  
  0% {left:0px;}  
  100% {left:200px;}  
} 
</style>  
</head>  
<body>  
<div></div>  
</body>  
</html>

以上程式碼可以使用animation-timing-function屬性設定動畫由慢到快進行過渡。

[HTML] 純文字檢視 複製程式碼執行程式碼
<!DOCTYPE html>     
<html>     
<head>     
<meta charset=" utf-8">     
<meta name="author" content="http://www.softwhy.com/" />     
<title>螞蟻部落</title>    
<style type="text/css">    
div{   
  width:100px;   
  height:100px;   
  background:red;   
  position:relative;   
         
  animation:ant-1 5s infinite alternate,ant-2 2s infinite alternate;   
  -webkit-animation:ant-1 5s infinite alternate,ant-2 2s infinite alternate;   
  -moz-animation:ant-1 5s infinite alternate,ant-2 2s infinite alternate;   
  -o-animation:ant-1 5s infinite alternate,ant-2 2s infinite alternate;   
     
  animation-timing-function:ease-in,ease-out; 
  -webkit-animation-timing-function:ease-in,ease-out; 
  -moz-animation-timing-function:ease-in,ease-out; 
  -o-animation-timing-function:ease-in,ease-out; 
}  
@keyframes ant-1{   
  0% {left:0px;}   
  100% {left:200px;}   
}   
@-webkit-keyframes ant-1{   
  0% {left:0px;}   
  100% {left:200px;}   
}   
@-moz-keyframes ant-1{   
  0% {left:0px;}   
  100% {left:200px;}   
}   
@-o-keyframes ant-1{   
  0% {left:0px;}   
  100% {left:200px;}   
}   


@keyframes ant-2{   
  0% {top:0px;}   
  100% {top:200px;}   
}   
@-webkit-keyframes ant-2{   
  0% {top:0px;}   
  100% {top:200px;}   
}   
@-moz-keyframes ant-2{   
  0% {top:0px;}   
  100% {top:200px;}   
}   
@-o-keyframes ant-2{   
  0% {top:0px;}   
  100% {top:200px;}   
}      
</style>   
</head>   
<body>   
<div></div>   
</body>   
</html>

以上程式碼可以通過animation-timing-function屬性設定兩個動畫的過渡型別。