DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
HTML5 + CSS3 動畫實例:讓人物頭像依序排列成一個圓環
編輯:HTML5詳解     

這是一個很適合拿來學 CSS3 動畫的範例。畫面初始只有中央的大頭貼,接著浮現一個笑臉標記,然後八個小頭像依序「飛」到各自的位置,最後排成一個完整的圓環;每個頭像右上角還會各自跳出一個綠色小圓點。

整個效果完全不需要 JavaScript,靠 HTML 結構 + CSS3 animation、transform 與 ::before 偽元素就能完成。

HTML 結構

一個 .stage 容器裡放中央的 .image,以及八個空白的 figure.avatar(頭像用 CSS 背景圖產生):

<div class='stage'>
  <div class='image'>
    <img src="profile.jpg">
    <div class='smiley'>
      <svg width="30px" height="30px">
        <path fill="#effedd" d="M15,0C6.7,0,0,6.7,0,15c0,8.3,6.7,15,15,15
          s15-6.7,15-15C30,6.7,23.3,0,15,0z M20.5,8c1.4,0,2.5,1.1,2.5,2.5
          c0,1.4-1.1,2.5-2.5,2.5S18,11.9,18,10.5C18,9.1,19.1,8,20.5,8z
          M10.5,8c1.4,0,2.5,1.1,2.5,2.5c0,1.4-1.1,2.5-2.5,2.5S8,11.9,8,10.5
          C8,9.1,9.1,8,10.5,8z M15,26c-5,0-9-4-9-9h3c0,3.3,2.7,6,6,6
          s6-2.7,6-6h3C24,22,20,26,15,26z" />
      </svg>
    </div>
  </div>

  <figure class='avatar'></figure>
  <figure class='avatar'></figure>
  <figure class='avatar'></figure>
  <figure class='avatar'></figure>
  <figure class='avatar'></figure>
  <figure class='avatar'></figure>
  <figure class='avatar'></figure>
  <figure class='avatar'></figure>
</div>

版面與頭像基本樣式

.stage 與 .avatar 都用「四邊設 0 + margin auto」的技巧做水平垂直置中,這比計算 top/left 簡單得多:

.stage {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  margin: auto;
  height: 460px;
  width: 480px;
}

.avatar {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  margin: auto;
  height: 64px;
  width: 64px;
  background-repeat: no-repeat;
  background-size: cover;
  border-radius: 50%;
  transform-origin: center;
}

每個頭像右上角的綠點,用 ::before 偽元素產生:

.avatar::before {
  content: '';
  position: absolute;
  top: -8%;
  right: -8%;
  height: 17px;
  width: 17px;
  background: #bec4bc;
  border-radius: 50%;
  border: 3px solid white;
}

核心:用旋轉 + 位移排成圓環

這是整个範例最巧妙的地方。要把物件均勻排在圓上,不需要算 sin/cos,只要:

  1. 先把元素旋轉某個角度(例如 45 度)
  2. 再沿 X 軸推開半徑距離(例如 208px)
  3. 最後反向旋轉回來(-45 度),讓頭像保持正立

八個頭像就是 45°、90°、135°… 一直到 360°,每個角度延遲 0.1 秒依序啟動,就形成依序飛出的效果:

.avatar:nth-of-type(1) {
  animation: ani1 2s 0.1s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes ani1 {
  0%, 20% {
    transform: rotate(45deg) translate(0) rotate(-45deg);
  }
  34%, 100% {
    transform: rotate(45deg) translate(208px) rotate(-45deg);
  }
}

.avatar:nth-of-type(2) {
  animation: ani2 2s 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes ani2 {
  0%, 20%   { transform: rotate(90deg) translate(0) rotate(-90deg); }
  34%, 100% { transform: rotate(90deg) translate(208px) rotate(-90deg); }
}

/* ani3 用 135deg、ani4 用 180deg……以此類推到 ani8 的 360deg */

每組 keyframes 裡的 0%, 20% 是「停在原點等待」,34%, 100% 是「到達終點並保持」。把這兩段拉開,就等於做出一段停頓後再出發的時間軸,而延遲(delay)則讓八個動畫錯開啟動。

那個 cubic-bezier(0.175, 0.885, 0.32, 1.275) 是重點:第二個控制點的數值大於 1,會讓元素衝過頭再彈回來,這就是「有彈性」的手感來源。

中央大頭貼的脈衝光環

.image::before {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  margin: auto;
  content: '';
  height: 100%;
  width: 100%;
  background: #f9fff7;
  border: 3px solid #e7f5d1;
  border-radius: 50%;
  animation: pulse 1s 1.4s ease-out;
  animation-iteration-count: 3;
}

@keyframes pulse {
  0%   { transform: scale(0.1, 0.1); opacity: 0.0; }
  50%  { opacity: 1.0; }
  100% { transform: scale(3); opacity: 0.0; }
}

彈跳與變色

笑臉與綠點共用一個 bounce 動畫,從 0 放大到 1.5 倍再收回到 1 倍,做出Q彈的出場:

.image .smiley {
  position: absolute;
  top: -8px; right: -8px;
  height: 64px; width: 64px;
  background: #b5e763;
  border-radius: 50%;
  border: 5px solid white;
  animation: bounce 5s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
  transform-origin: center;
  z-index: 1;
}

@keyframes bounce {
  0%        { transform: scale(0); }
  5%        { transform: scale(1.5); }
  10%, 100% { transform: scale(1); }
}

/* 綠點的顏色從灰漸變為綠 */
@keyframes colorchange {
  0%   { background: #bec4bc; }
  100% { background: #b5e763; }
}

/* 讓每個頭像的綠點在不同時間彈出並變色 */
.avatar:nth-of-type(4)::before,
.avatar:nth-of-type(2)::before {
  animation: bounce 3s 1.8s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards,
             colorchange 1s 1.8s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}
/* 其餘頭像分別使用 2s、2.8s、3s、3.2s、3.8s、4s 的延遲 */

這裡有個值得學的技巧:animation 可以用逗號串接多組動畫,讓同一個元素同時跑「彈跳」與「變色」兩件事,且各自擁有獨立的延遲時間。

掛上頭像圖片

.avatar:nth-of-type(1) { background-image: url("128.jpg"); }
.avatar:nth-of-type(2) { background-image: url("rasagy.jpg"); }
.avatar:nth-of-type(3) { background-image: url("geeftvorm.jpg"); }
.avatar:nth-of-type(4) { background-image: url("VinThomas.jpg"); }
.avatar:nth-of-type(5) { background-image: url("ladylexy.jpg"); }
.avatar:nth-of-type(6) { background-image: url("claudioguglieri.jpg"); }
.avatar:nth-of-type(7) { background-image: url("jina.jpg"); }
.avatar:nth-of-type(8) { background-image: url("peterme.jpg"); }

這個範例的三個可複用觀念

  • 極座標思維:rotate(θ) translateX(r) rotate(-θ) 是排圓形佈局最簡潔的寫法,改一個半徑就能縮放整圈。
  • 用 delay 編排時間軸:不需要 setTimeout,只要調整 animation-delay 就能做出「依序登場」的敘事節奏。
  • 用 forwards 保留終態:animation-fill-mode: forwards 讓動畫結束後停在最後一幀,元素才不會「啪」地跳回原位。

把頭像換成節點圖示、把圓環換成半圓,這套寫法就能直接改造成流程圖或團隊介紹頁的進場動畫。

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