Carl Philipe Brenner 網站的網格項懸停效果:邊框四條邊依次順時針「繞角」展開,同時盒子背景淡出、文字變色、陰影變化。核心是 **SVG 線條動畫 + CSS 過渡**,零 JS 運行時計算。
<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>
.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;
}
邊長 L,線條總長 3L,實線段 L,間隙 2L。初始 `dashoffset=0` 時,實線段在線條起始端。需計算初始偏移使實線段落在邊框位置,或直接用 `transform: translate` 移動整條線(本例採用 transform,更直觀)。
/* 時序圖 */
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 : 所有動畫完成
錯開延遲創造「邊框先動、背景再退、文字最後變色」的層次感。
<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 創造韻律**。此模式可擴展至載入動畫、按鈕交互、圖表繪製等場景。