利用css3製作旋轉動畫

  利用css3功能強大,我們可以直接完成旋轉動畫的製作,而跳過複雜的javascript。

  html代碼如下:demo01.html

  <!DCTYPE html>
      <head>
          <meta type="utf-8"/>
          <title>旋轉動畫</title>
          <link rel="stylesheet" type="text/css" href="demo01.css">
      </head>
      <body>
          <div class="container">
              <div id="around">
                  <figure>1<figure>
                  <figure>2<figure>
                  <figure>3<figure>
                  <figure>4<figure>
                  <figure>5<figure>
                  <figure>6<figure>
                  <figure>7<figure>
                  <figure>8<figure>
                  <figure>9<figure>
              </div>
          </div>
      </body>
  </html>
  
  
  css代碼如下:demo01.css
  .container{
      width:210px;
      height:140px;
      position:relative;
      //3d視距,當爲元素定義 perspective 屬性時,其子元素會獲得透視效果,而不是元素本身
      perspective:1000px;
      margin:50px auto 40px;
  }
  #around{
      width:100%;
      height:100%;
      //規定如何在 3D 空間中呈現被嵌套的元素
      transform-style:preserve-3d;
      position:absolute;
      //定義動畫
      animation:myMove 10s infinite;
  }
  #around figure{
      display:block;
      position:relative;
      width:186px;
      height:116px;
      left:10px;
      top:10px;
      border:2px solid black;
      text-align:center;
      color:white;
      font-weight:bold;
      //與高度相等,使內容垂直居中顯示
      line-height:116px;
  }
  #around figure:nth-child(1){
      transform: rotateY(0deg) translateZ(288px);
      background-color:red;
  } 
   #around figure:nth-child(2){
      transform: rotateY(40deg) translateZ(288px);
      background-color:orange;
  } 
   #around figure:nth-child(3){
      transform: rotateY(80deg) translateZ(288px);
      background-color:yellow;
  } 
   #around figure:nth-child(4){
      transform: rotateY(120deg) translateZ(288px);
      background-color:green;
  } 
   #around figure:nth-child(5){
      transform: rotateY(160deg) translateZ(288px);
      background-color:darkgreen;
  } 
   #around figure:nth-child(6){
      transform: rotateY(200deg) translateZ(288px);
      background-color:blue;
  } 
   #around figure:nth-child(7){
      transform: rotateY(240deg) translateZ(288px);
      background-color:purple;
  } 
   #around figure:nth-child(8){
      transform: rotateY(280deg) translateZ(288px);
      background-color:navy;
  }
   #around figure:nth-child(9){
      transform: rotateY(320deg) translateZ(288px);
      background-color:pink;
  }
  #keyframes myMove{
    from{
      transform: rotateY(360deg);    
    }
    to{
     transform: rotateY(0deg);
    }
}


 完成後的圖示如下:

wKiom1diFTOiL2EqAAAoHoPUltI870.png-wh_50

圖片可以自動旋轉。修改animation屬性裏的值可以實現無限循環(infinite)和多次循環(ease n).

如:animation:myMove 10s ease 2. myMove爲動畫函數,10s爲完成動畫所用時間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章