首頁 > web前端 > css教學 > 主體

手把手教你怎麼使用CSS3實現動畫效果(程式碼分享)

奋力向前
發布: 2021-08-17 09:33:31
轉載
3280 人瀏覽過

之前的文章《H5篇:頁面中實作動畫的幾種方式? (附程式碼)》中,帶大家了解頁面中實作動畫的幾種方式。下面這篇文章跟大家介紹怎麼使用CSS3來實現一個簡單漂亮的動畫效果,我們一起看看

#複習下css3 的動畫, 都快不會寫了,那會兒挺喜歡flash 的,可惜了時代在前進。寫這裡就當是文件看吧

瀏覽器支援

Internet Explorer 10、Firefox 以及 Opera 支援 animation 屬性。

Safari 和 Chrome 支援替代的 -webkit-animation 屬性。

註解:Internet Explorer 9 以及更早的版本不支援 animation 屬性。

定義和用法

animation屬性是一個簡寫屬性,用於設定六個動畫屬性:

  • #animation-name

  • animation-duration

  • #animation-timing-function animation-delay

  • animation-iteration-count animation-direction

語法

#
animation: name duration timing-function delay iteration-count direction;
登入後複製
##值描述備註animation-timing-function規定動畫的速度曲線可取值為linear ,ease(淡入淡出),ease-in,ease-out ,ease-in-out,cubic-bezier(n, n, n, n)animation -play-state規定動畫是否正在運作或暫停。 paused 表示暫停狀態,running 表示運行狀態animation-name#規定需要綁定到選擇器的keyframe 名稱#@keyframe { 從 {opcity:0} to {opcity:1}}#animation-iteration-count規定動畫應該播放的次數可選值為  infinite(無限次)n(例如5 次)#animation-fill-mode動畫在播放之前或之後,其動畫效果是否可見。 none(預設) / forwards(動畫完成後) / backwards(在動畫顯示之前) / both(兩者);animation-duration規定完成動畫所花費的時間,以秒或毫秒計必須規定否則,不執行動畫

animation-direction規定是否應該輪流反向播放動畫

預設值normal,alternate 為動畫應該輪流反向播放。左右左

  • animation-delay

  • 規定在動畫開始之前的延遲
  • 定義動畫開始前等待的時間,以秒或毫秒計。預設值是 0。單位為s


  • 關於keyframe的定義

  • Firefox支援替代的@-moz -keyframes規則;

手把手教你怎麼使用CSS3實現動畫效果(程式碼分享)Opera支援替代的@-o-keyframes規則;

##Safari和Chrome支援替代的@-webkit-keyframes規則;

取值支援0-100% 和from,to。

@keyframes move {
  0% {
    top: 0px;
    left: 0px;
  }
  25% {
    top: 200px;
    left: 200px;
  }
  50% {
    top: 100px;
    left: 100px;
  }
  75% {
    top: 200px;
    left: 200px;
  }

  100% {
    top: 0px;
    left: 0px;
  }
}

@keyframes move {
  from {
    top: 0px;
    left: 0px;
  }
  to {
    top: 0px;
    left: 100px;
  }
}
登入後複製

demo 寫了一個例子,地球繞太陽轉######################以下是程式碼#########
<!-- html 部分 -->
<div style="width:700px; ">
  <div class="t">
    <div class="t1"></div>
  </div>
</div>
登入後複製
/* css 部分 */
@keyframes t {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@-webkit-keyframes t {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.t {
  height: 500px;
  width: 500px;
  position: relative;
  border-radius: 50%;
  transform: scale(.8);
  border: 1px solid #dedede;
  &::before {
    content: "";
    width: 50px;
    height: 50px;
    background: radial-gradient(72px, #f00, #ff0, #080);
    border-radius: 50%;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-top: -25px;
    margin-left: -25px;
    box-shadow: 0 0 37px #fcff4a;
  }
  .t1 {
    height: 20px;
    border-radius: 50%;
    width: 20px;
    margin-top: -10px;
    top: 50%;
    left: -10px;
    background: radial-gradient(26px, #0082ff, #184608, #003ade);
    position: absolute;
    animation: t 3s infinite linear;
    transform-origin: 260px center;
  }
}

</style>
登入後複製
###還有一個demo ,在這裡https://k-ui.cn######推薦學習###CSS3影片教學#######

以上是手把手教你怎麼使用CSS3實現動畫效果(程式碼分享)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:chuchur.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!