萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

純 CSS 實現垂直時間軸:打造優雅的任務進度展示

效果預覽

一款採用藍色主題的垂直時間軸,適合任務排程、專案里程碑、文章歸檔等場景。無需 JavaScript,僅靠 CSS 完成結構佈局與懸停動畫。

垂直時間軸效果圖

核心 HTML 結構

語義化標籤:<time> 標記時間,<ul> 承載時間點列表,<div class="cbp_tmlabel"> 包裹內容區塊。

<ul class="cbp_tmtimeline">
  <li>
    <time class="cbp_tmtime" datetime="2013-04-10 18:30">
      <span>4/10/13</span> <span>18:30</span>
    </time>
    <div class="cbp_tmicon cbp_tmicon-phone"></div>
    <div class="cbp_tmlabel">
      <h2>事件標題</h2>
      <p>詳細描述內容...</p>
    </div>
  </li>
  <!-- 更多 li 節點 -->
</ul>

關鍵 CSS 技術解析

1. 中央時間軸線

.cbp_tmtimeline {
  position: relative;
  padding: 2em 0;
  list-style: none;
}
.cbp_tmtimeline::before {
  content: '';
  position: absolute;
  top: 0; bottom: 0;
  left: 50%; margin-left: -2px;
  width: 4px; background: #3498db; /* 主題色 */
}

2. 時間節點圖標定位

.cbp_tmicon {
  position: absolute;
  left: 50%; margin-left: -20px;
  width: 40px; height: 40px;
  border-radius: 50%;
  background: #fff;
  border: 4px solid #3498db;
  z-index: 1;
}
.cbp_tmicon-phone::before { content: "\e900"; /* 圖標字體 */ }

3. 內容卡片交錯佈局

.cbp_tmlabel {
  position: relative;
  width: 45%;
  padding: 1.5em;
  border-radius: 4px;
  background: #f8f9fa;
  box-shadow: 0 2px 6px rgba(0,0,0,.1);
}
.cbp_tmtimeline li:nth-child(odd) .cbp_tmlabel { left: 0; }
.cbp_tmtimeline li:nth-child(even) .cbp_tmlabel { left: 55%; }

4. 連接線與三角指標

.cbp_tmlabel::before {
  content: '';
  position: absolute;
  top: 1.5em;
  width: 0; height: 0;
  border: 10px solid transparent;
}
li:nth-child(odd) .cbp_tmlabel::before {
  right: -20px; border-left-color: #f8f9fa;
}
li:nth-child(even) .cbp_tmlabel::before {
  left: -20px; border-right-color: #f8f9fa;
}

5. 入場動畫(可選增強)

@keyframes fadeSlideUp {
  from { opacity: 0; transform: translateY(30px); }
  to   { opacity: 1; transform: translateY(0); }
}
.cbp_tmtimeline li {
  animation: fadeSlideUp 0.6s ease-out backwards;
}
.cbp_tmtimeline li:nth-child(n) { animation-delay: calc(var(--i) * 0.1s); }

響應式調適

@media (max-width: 640px) {
  .cbp_tmtimeline::before { left: 20px; }
  .cbp_tmicon { left: 20px; margin-left: 0; }
  .cbp_tmlabel {
    width: calc(100% - 60px);
    left: 60px !important;
  }
  .cbp_tmlabel::before { left: -20px !important; border-right-color: #f8f9fa !important; }
}

擴展建議

  • 搭配 IntersectionObserver 實現滾動觸發動畫。
  • 使用 CSS 變數管理主題色、間距,支援多主題切換。
  • 內容區塊可嵌入圖片、影片、標籤等富媒體。

完整代碼獲取

本文提供核心邏輯,完整可運行代碼(含圖標字體、動畫細節)請參考原文附件或至 CodePen 搜尋 "Vertical Timeline CSS Only"。

copyright © 萬盛學電腦網 all rights reserved