CSS 邊框動畫

瞭解在 CSS 中為邊框加上動畫效果的幾種方法

設定邊框

設定元素邊框的方法有幾種:border、outline 和 box-shadow。如 Stephanie Eckles 的「新增元素邊框的 3 種 CSS 方法」一文所述,每種方法各有優缺點,尤其是在為邊框加上動畫效果時。不使用適當 CSS border 的主要原因,是為了製作動畫。

使用 outline-offset 的邊框動畫,作者:Kevin J. Powell

最近引起我注意的一篇文章是《Fantastic CSS border animation》,作者 Coco 在文中探討了更多選項。他們使用 ::before 和 ::after 插入生成內容,建立假邊框,然後加上動畫效果。

最讓我印象深刻的是文章中使用的輔助動畫圖表。這些資訊有助於說明為達到預期效果,實際採取了哪些措施。

Border Animations using generated content by Coco

白色圖層和彩色線條都是生成內容。只要淡入和淡出白色圖層,就能清楚瞭解圖層的堆疊方式和動畫的運作方式。

保留方塊模型

使用生成內容模擬邊框的缺點是,最終會產生損壞的方塊模型:內容現在可以遮蓋假邊框,因為上述「邊框」是在下方繪製。如要緩解,您必須將所需的 border-width 套用為 padding。

如要設定真正的邊框,並保留方塊模型的運作方式,您可以使用多個背景,然後將其延伸至邊框區域。

基本概念

首先,建立虛線邊框並新增多個背景。

/* Size of the border */
--border-size: 0.5rem;

/* Create a dotted border */
border: var(--border-size) dotted lime;

/* Create two background layers:
   1. A white semi-transparent
   2. A layer with the colored boxes
 */
background-image:
  linear-gradient(to right, rgb(255 255 255 / 0.5), rgb(255 255 255 / 0.5)),

  conic-gradient(
    from 45deg,
    #d53e33 0deg 90deg,
    #fbb300 90deg 180deg,
    #377af5 180deg 270deg,
    #399953 270deg 360deg
  )
;

使用background-origin調整背景大小

如您所見,這裡的背景有些奇怪:背景繪製在邊框中,但 conic-gradient 似乎完全錯誤。這其實是預期行為:根據預設,背景圖片不會繪製到邊框中,因為其原點是元素的 padding-box。畢竟,為了建立邊框,設定的背景圖片會在邊框本身重複,產生奇怪的視覺效果。

如要解決這個問題,您需要拉伸背景,使其也佔用邊框的大小。您可以手動拉伸及重新定位背景,但最好使用 background-origin 屬性,根據 border-box 調整背景大小。

Browser Support

  • Chrome: 1.
  • Edge: 12.
  • Firefox: 4.
  • Safari: 3.

Source

錯誤做法
/* Manually add or offset the size of the border where needed */
background-position: calc(var(--border-size) * -1) calc(var(--border-size) * -1);
background-size: calc(var(--border-size) * 2 + 100%) calc(var(--border-size) * 2 + 100%);
正確做法
background-origin: border-box;

只要加上這項設定,一切看起來就會好得多:

使用 background-clip 縮小白色背景圖層

背景現在會佔用所有空間,因此半透明圖層需要再次縮小。除了再次調整 background-size 之外,還有更簡單的方法:使用 background-clip 並設為 padding-box。這樣一來,背景就不會再繪製在邊框區域下方。

Browser Support

  • Chrome: 1.
  • Edge: 12.
  • Firefox: 4.
  • Safari: 5.

Source

background-clip:
  padding-box, /* Clip white semi-transparent to the padding-box */
  border-box /* Clip colored boxes to the border-box (default) */
;

最後,將邊框設為 transparent,即可完整呈現效果。

border: 0.3rem dotted transparent;

動畫

如要還原邊框動畫,可以操控 conic-gradient 的起始角度。

--angle: 0deg;
conic-gradient(
  from var(--angle),
  #d53e33 0deg 90deg,
  #fbb300 90deg 180deg,
  #377af5 180deg 270deg,
  #399953 270deg 360deg
);

有了 @property,在支援這項功能的瀏覽器中,這項操作就變得非常簡單:

Browser Support

  • Chrome: 85.
  • Edge: 85.
  • Firefox: 128.
  • Safari: 16.4.

Source

@property --angle {
  syntax: "<angle>";
  initial-value: 0deg;
  inherits: false;
}

@keyframes rotate {
  to {
    --angle: 360deg;
  }
}

合併後的程式碼如下:

附加內容:border-image

先前介紹過繪製漸層邊框的方法,是使用 CSS border-image。

Browser Support

  • Chrome: 16.
  • Edge: 12.
  • Firefox: 15.
  • Safari: 6.

Source

由於您不需要處理重疊的背景,因此可以簡化程式碼。動畫可以按照先前的方式套用。

/* Create a border */
border: 0.5rem solid transparent;

/* Paint an image in the border */
border-image:
  conic-gradient(
    from var(--angle),
    #d53e33 0deg 90deg,
    #fbb300 90deg 180deg,
    #377af5 180deg 270deg,
    #399953 270deg 360deg
  ) 1
;

不過,您會發現有幾項功能無法再透過這種方式運作:

  • border-image 不會跟隨 border-radius,一律會保持矩形。
  • 將 border-image-slice 設為填滿時,border-image 不會繪製在設定的 background 下方,而是繪製在上方。如果您希望背景半透明,這可能會造成問題。

結語

在 CSS 中,有許多方法可以為邊框加上動畫效果。視用途而定,您可能會偏好其中一種。