CSS3 animation-direction

admin發表於2018-09-06

animation-direction屬性用於設定animation動畫是否可以反向運動。

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

語法結構:

[CSS] 純文字檢視 複製程式碼
animation-direction:normal | alternate [ , normal | alternate ]*

引數解析:

(1).normal:動畫正常方向執行。

(2).alternate:正常方向與反方向交替。

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

程式碼例項:

[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; 
  -webkit-animation:theanimation 5s infinite; 
  -moz-animation:theanimation 5s infinite; 
  -o-animation:theanimation 5s infinite; 
   
  animation-direction:alternate;
  -webkit-animation-direction:alternate;
  -moz-animation-direction:alternate;
  -o-animation-direction:alternate;
} 
@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;} 
} 
@-ms-keyframes theanimation{ 
  0% {left:0px;} 
  100% {left:200px;} 
} 
</style> 
</head> 
<body> 
<div></div> 
</body> 
</html>

以上程式碼可以設定動畫正常方向與反方向交替運動。

[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,ant-2 2s infinite;  
  -webkit-animation:ant-1 5s infinite,ant-2 2s infinite;  
  -moz-animation:ant-1 5s infinite,ant-2 2s infinite;  
  -o-animation:ant-1 5s infinite,ant-2 2s infinite;  
   
  animation-direction:alternate,normal;
  -webkit-animation-direction:alternate,normal;
  -moz-animation-direction:alternate,normal;
  -o-animation-direction:alternate,normal;
} 
@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;}  
}  
@-ms-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;}  
}  
@-ms-keyframes ant-2{  
  0% {top:0px;}  
  100% {top:200px;}  
}  
</style>  
</head>  
<body>  
<div></div>  
</body>  
</html>

以上程式碼可以分別設定兩個動畫一個是兩個方向交替執行,一個只能夠以正常方向執行。