萬盛學電腦網

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

SVG + CSS3 實現炫酷邊框動畫:線條繞角轉場、顏色過渡與交互增強

效果拆解

Carl Philipe Brenner 網站的網格項懸停效果:邊框四條邊依次順時針「繞角」展開,同時盒子背景淡出、文字變色、陰影變化。核心是 **SVG 線條動畫 + CSS 過渡**,零 JS 運行時計算。

核心原理:stroke-dasharray + stroke-dashoffset + transform

  • 每條邊是一條 **長度 = 3 × 邊長** 的虛線(`stroke-dasharray: 300 200` 表示 300px 實線 + 200px 間隙)
  • 初始 `stroke-dashoffset` 讓「中間一段實線」正好落在邊框位置
  • Hover 時 `transform: translateX/Y` 移動線條,實線段滑出、間隙滑入,視覺上邊框「繞角轉移」
  • 四條邊分別設定不同 `translate` 方向與 `transition-delay`,形成連續波浪

完整實現代碼

HTML 結構

<div class="box">
  <svg class="box-svg" viewBox="0 0 300 460">
    <line class="line top"    x1="0"   y1="0"   x2="900" y2="0" />   
    <line class="line right"  x1="300" y1="460" x2="300" y2="1380"/>  
    <line class="line bottom" x1="300" y1="460" x2="-600" y2="460"/>  
    <line class="line left"   x1="0"   y1="0"   x2="0"   y2="-920"/>  
  </svg>
  <div class="box-content">
    <h3>D</h3>
    <span>2012</span>
    <p>Broccoli, Asparagus, Curry</p>
  </div>
</div>

CSS 核心樣式

.box {
  position: relative;
  width: 300px; height: 460px;
  background: rgba(255,255,255,1);
  display: inline-block;
  margin: 0 10px;
  cursor: pointer;
  color: #2c3e50;
  box-shadow: inset 0 0 0 3px #2c3e50;
  transition: background 0.4s 0.5s; /* 背景淡出延遲 0.5s,等邊框動畫先跑 */
}
.box:hover {
  background: rgba(255,255,255,0);
  transition-delay: 0s; /* 滑出時背景立即淡出 */
}

.box-svg {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none; /* SVG 不攔截滑鼠事件 */
}

.box-svg .line {
  stroke-width: 3;
  stroke: #ecf0f1;
  fill: none;
  transition: all 0.8s ease-in-out;
  /* 初始 dasharray:實線段 = 邊長,間隙 = 2×邊長 */
}
.box-svg .top,
.box-svg .bottom { stroke-dasharray: 330 240; }  /* 寬 300 → 330/240 經微調 */
.box-svg .left,
.box-svg .right  { stroke-dasharray: 490 400; }  /* 高 460 → 490/400 經微調 */

/* Hover 觸發:四條邊分別向四個方向平移 */
.box:hover .line.top    { transform: translateX(-600px); transition-delay: 0.1s; }
.box:hover .line.bottom { transform: translateX(600px);  transition-delay: 0.1s; }
.box:hover .line.left   { transform: translateY(920px);  transition-delay: 0.1s; }
.box:hover .line.right  { transform: translateY(-920px); transition-delay: 0.1s; }

/* 內容文字同步變色 */
.box h3, .box span, .box p {
  transition: color 0.4s 0.5s;
}
.box:hover h3, .box:hover span, .box:hover p {
  color: #fff;
  transition-delay: 0s;
}

/* 文字樣式 */
.box h3 {
  font-family: "Ruthie", cursive;
  font-size: 180px; line-height: 370px;
  margin: 0; font-weight: 400; width: 100%;
}
.box span {
  display: block; font-weight: 400;
  text-transform: uppercase; letter-spacing: 1px; font-size: 13px; padding: 5px;
}

關鍵技術細節

1. 線條長度計算

邊長 L,線條總長 3L,實線段 L,間隙 2L。初始 `dashoffset=0` 時,實線段在線條起始端。需計算初始偏移使實線段落在邊框位置,或直接用 `transform: translate` 移動整條線(本例採用 transform,更直觀)。

2. viewBox 與座標系統

  • `viewBox="0 0 300 460"` 定義 SVG 內部座標系,與 `.box` 尺寸 1:1 對應
  • 線條座標超出 viewBox 範圍(如 x2=900),超出部分被裁切,不影響視覺

3. 動畫時序設計

/* 時序圖 */
0ms     : 用戶 hover
0-100ms : 等待(transition-delay: 0.1s)
100-900ms: 邊框線條位移動畫 (0.8s ease-in-out)
500ms   : 背景開始淡出 (transition-delay: 0.5s)
900ms   : 邊框動畫結束,背景淡出中
900-1300ms: 文字顏色變白 (0.4s)
1300ms  : 所有動畫完成

錯開延遲創造「邊框先動、背景再退、文字最後變色」的層次感。

4. 效能優化

  • `transform` + `opacity` 僅觸發合成,GPU 加速
  • `pointer-events: none` 讓 SVG 不阻擋滑鼠事件
  • `will-change: transform, opacity, background` 可選,提前提升圖層

瀏覽器支援與降級

  • SVG + CSS 動畫:Chrome 4+, Firefox 5+, Safari 4+, Edge 12+, IE **不支援** SVG 上的 CSS transform 動畫
  • 2. IE 降級:用 `
    ` 模擬四條邊,配合 `width/height` 動畫(較卡頓)或直接無動畫顯示邊框 3. `@supports (transform: translateX(0))` 包裹現代寫法

擴展變體

不同幾何形狀

  • 圓形:`` + `stroke-dasharray` + `transform: rotate()`
  • 多邊形:`` 點座標動畫(需 SMIL 或 JS)

點擊觸發(而非 hover)

<input type="checkbox" id="toggle" class="toggle-input">
<label for="toggle" class="box">...</label>

.toggle-input:checked + .box { /* hover 狀態樣式 */ }
.toggle-input:checked + .box .line.top    { transform: translateX(-600px); } /* ... */

總結

SVG 線條配合 CSS `stroke-dasharray`、`transform`、`transition` 可創造極具表現力的邊框動畫,完全聲明式、零 JS 運行時、GPU 加速。核心技巧:**線條長度 3× 邊長、實線段 1×、間隙 2×、transform 位移實現「繞角」視覺、錯開 transition-delay 創造韻律**。此模式可擴展至載入動畫、按鈕交互、圖表繪製等場景。

copyright © 萬盛學電腦網 all rights reserved