DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
3款CSS3載入動畫實作:脈衝圓點、擴展進度條與逐格填色
編輯:CSS詳解     

載入動畫是少數「純粹用 CSS 就能做得很好看」的題材。這裡有三個可以直接搬去用的實例,分別示範了 animation-delay 的錯開節奏、transform: scale() 的縮放,以及 opacity 的逐格淡入。

實例一:脈衝圓點進度動畫

HTML 結構

一個 ul 列表,每個 li 裡放兩個 div:一個是「球」(ball,負責放大出現),一個是「脈衝」(pulse,負責向外擴散的光環)。

<ul id="progress">
  <li>
    <div id="layer1" class="ball"></div>
    <div id="layer7" class="pulse"></div>
  </li>
  <li>
    <div id="layer2" class="ball"></div>
    <div id="layer8" class="pulse"></div>
  </li>
  <!-- layer3~layer5 依此類推 -->
</ul>

CSS 要點

ul#progress {
  list-style: none;
  width: 125px;
  margin: 0 auto;
  padding: 50px 0;
}

ul#progress li {
  float: left;
  position: relative;
  width: 15px;
  height: 15px;
  margin-left: 10px;
  border-radius: 50px;
  border-left: 1px solid #111;
  border-top: 1px solid #111;
  border-right: 1px solid #333;
  border-bottom: 1px solid #333;
  background: #000;
}

ul#progress li:first-child { margin-left: 0; }

.ball {
  background-color: #2187e7;
  background-image: linear-gradient(90deg, #2187e7 25%, #a0eaff);
  width: 15px;
  height: 15px;
  border-radius: 50px;
  transform: scale(0);
  animation: loading 1s linear forwards;
}

.pulse {
  width: 15px;
  height: 15px;
  border-radius: 30px;
  border: 1px solid #00c6ff;
  box-shadow: 0 0 5px #00c6ff;
  position: absolute;
  top: -1px;
  left: -1px;
  transform: scale(0);
  animation: pulse 1s ease-out;
}

整個效果之所以看起來像「依序前進」,關鍵就在這一段延遲設定——每往後一個圓點,就晚 0.5 秒啟動:

#layer1 { animation-delay: 0.5s; }
#layer2 { animation-delay: 1s; }
#layer3 { animation-delay: 1.5s; }
#layer4 { animation-delay: 2s; }
#layer5 { animation-delay: 2.5s; }

#layer7  { animation-delay: 1.5s; }
#layer8  { animation-delay: 2s; }
#layer9  { animation-delay: 2.5s; }
#layer10 { animation-delay: 3s; }
#layer11 { animation-delay: 3.5s; }

@keyframes loading {
  0%   { transform: scale(0, 0); }
  100% { transform: scale(1, 1); }
}

@keyframes pulse {
  0%   { transform: scale(0);    opacity: 0; }
  10%  { transform: scale(1);    opacity: 0.5; }
  50%  { transform: scale(1.75); opacity: 0; }
  100% { transform: scale(0);    opacity: 0; }
}

注意 pulse 的時間軸比 ball 晚 1 秒啟動,光環才會在球出現之後才蕩開。

實例二:由左至右填滿的進度條

結構簡單到不可思議——一個容器,裡面一個 span:

<div id="content">
  <span class="expand"></span>
</div>
#content {
  width: 100%;      /* 滿版 */
  height: 5px;
  margin: 50px auto;
  background: #000;
}

.expand {
  width: 100%;
  height: 1px;
  margin: 2px 0;
  background: #2187e7;
  position: absolute;
  box-shadow: 0 0 10px 1px rgba(0, 198, 255, 0.7);
  animation: fullexpand 10s ease-out;
}

@keyframes fullexpand {
  0%   { width: 0; }
  100% { width: 100%; }
}

動畫的對象是 width 本身,搭配一點外發光陰影,視覺上就很有科技感。

實例三:逐格淡入的填色進度條

這版改用 opacity 做切換,效果接近第一個例子,但更簡潔:

<ul id="loadbar">
  <li><div id="layerFill1" class="bar"></div></li>
  <li><div id="layerFill2" class="bar"></div></li>
  <!-- layerFill3 ~ layerFill10 依此類推 -->
</ul>
ul#loadbar {
  list-style: none;
  width: 140px;
  margin: 0 auto;
  padding: 50px 0 75px;
}

ul#loadbar li {
  float: left;
  position: relative;
  width: 11px;
  height: 26px;
  margin-left: 1px;
  border-left: 1px solid #111;
  border-top: 1px solid #111;
  border-right: 1px solid #333;
  border-bottom: 1px solid #333;
  background: #000;
}

ul#loadbar li:first-child { margin-left: 0; }

.bar {
  background-color: #2187e7;
  background-image: linear-gradient(45deg, #2187e7 25%, #a0eaff);
  width: 11px;
  height: 26px;
  opacity: 0;
  animation: fill 0.5s linear forwards;
}

#layerFill1  { animation-delay: 0.5s; }
#layerFill2  { animation-delay: 1s; }
#layerFill3  { animation-delay: 1.5s; }
#layerFill4  { animation-delay: 2s; }
#layerFill5  { animation-delay: 2.5s; }
#layerFill6  { animation-delay: 3s; }
#layerFill7  { animation-delay: 3.5s; }
#layerFill8  { animation-delay: 4s; }
#layerFill9  { animation-delay: 4.5s; }
#layerFill10 { animation-delay: 5s; }

@keyframes fill {
  0%   { opacity: 0; }
  100% { opacity: 1; }
}

三個實例的共同公式

拆到底其實只有三個要素:一個能被動畫的屬性(scale、width 或 opacity)、一組 keyframes,以及用 animation-delay 製造的時間差。把這三者排列組合,就能生出各式各樣的載入效果。原文中的 -moz-/-webkit- 前綴在今日的瀏覽器環境下已非必要,保留是為了對照歷史寫法。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved