天天看點

css簡易動畫制作

簡單2d動畫轉換執行個體:由方形到圓的過渡以及顔色變化。

動畫(animation)

1. 什麼是動畫

  動畫是CSS3中最具颠覆性的特征之一,可通過設定多個節點來精确的控制一個或者一組動畫,進而實作複雜的動畫效果

2. 動畫的基本使用

先定義動畫在調用定義好的動畫

3. 文法格式(定義動畫)

@keyframes動畫名稱 
{
 0% {width: 100px; 
 100% {width: 200px
}    
           

4. 文法格式(使用動畫)

div {/* 調用動畫 */    animation-name: 動畫名稱;/* 持續時間 */animation-duration: 持續時間;}
           

5. 動畫序列

0% 是動畫的開始,100 % 是動畫的完成,這樣的規則就是動畫序列在 @keyframs 中規定某項 CSS 樣式,就由建立目前樣式逐漸改為新樣式的動畫效果動畫是使元素從一個樣式逐漸變化為另一個樣式的效果,可以改變任意多的樣式任意多的次數用百分比來規定變化發生的時間,或用from和to,等同于 0% 和 100%

6. 代碼示範

<style>
div 
{width: 100px;height: 100px;background-color: aquamarine;animation-name: move;animation-duration: 0.5s;    }
@keyframesmove
{
0% {transform: translate(0px)
}
100% {transform: translate(500px, 0)}    
}  
</style>
           

代碼執行個體:

正方形到圓形過渡動畫實作

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>動畫</title>
</head>
<style>
    body{
        margin: 0px;
    padding: 0px;   
     background-color: aqua;  width: 1400px;height: 1000px;  
    }
.cc{
    width:100px;
    height:100px;
    background-color: palevioletred;
    border-radius: 50%;
    position:absolute;
    margin-top: 10px;
    margin-left: 0px;
    animation: move 4s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
    animation-direction: alternate;
}
@keyframes move {
    0% {
        border-radius: 50px;
        background-color: rgb(238, 42, 42);
        transform: translate(0px, 500px);
    }
    40% {
        width: 100px;
        height: 100px;
        border-radius: 10px;
        background-color: rgb(238, 42, 42);
        transform: translate(600px, 500px);
    }
    45% {
        width: 100px;
        height: 100px;
        background-color: rgb(238, 42, 42);
        transform: translate(650px, 500px);
    }
    50% {
        width: 100px;
        height: 100px;
        background-color: rgb(238, 42, 42);
        transform: translate(700px, 500px);
    }
    55% {
        width: 100px;
        height: 100px;
        background-color: rgb(204, 238, 12);
        transform: translate(750px, 500px);
    }
    60% {
        width: 100px;
        height: 100px;
        border-radius: 10px;
        background-color: rgb(204, 238, 12);
        transform: translate(800px, 500px);
    }
    100% {
        border-radius: 50px;
        background-color: rgb(204, 238, 12);
        transform: translate(1400px, 500px);
    }
}
</style>

<body>
  <div class="cc"></div>  
</body>
</html>