萬盛學電腦網

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

CSS3 按鈕動畫實戰:關鍵幂動畫、變換、陰影打造極致交互體驗

CSS3 按鈕動畫實戰:從基礎樣式到關鍵幂脈動效果完整教程

優秀的按鈕動畫能顯著提升用戶體驗與轉化率。本文從零構建一個具有脈動呼吸效果的 3D 立體按鈕,涵蓋漸變背景、陰影分層、關鍵幂動畫、性能優化全流程。

基礎按鈕樣式:3D 立體質感

.btn {
    /* 基礎佈局 */
    display: inline-block;
    padding: 12px 28px;
    font-size: 16px;
    font-weight: 600;
    line-height: 1.5;
    text-align: center;
    text-decoration: none;
    color: #fff;
    cursor: pointer;
    border: none;
    border-radius: 8px;
    
    /* 3D 立體效果:分層陰影 */
    background: linear-gradient(180deg, #4a90e2 0%, #357abd 100%);
    box-shadow: 
        0 4px 0 0 #2a68a0,           /* 底部立體邊 */
        0 6px 12px rgba(0,0,0,0.15), /* 擴散陰影 */
        inset 0 1px 0 rgba(255,255,255,0.2); /* 內部高光 */
    
    /* 過渡基礎 */
    transition: 
        transform 0.1s ease,
        box-shadow 0.1s ease,
        background 0.2s ease;
    
    /* 文字陰影增強立體感 */
    text-shadow: 0 -1px 1px rgba(0,0,0,0.2);
}

/* 懸停:上浮 + 陰影增強 */
.btn:hover {
    transform: translateY(-2px);
    box-shadow: 
        0 6px 0 0 #2a68a0,
        0 8px 16px rgba(0,0,0,0.2),
        inset 0 1px 0 rgba(255,255,255,0.25);
    background: linear-gradient(180deg, #5ba0f2 0%, #458ad0 100%);
}

/* 按下:下沉 + 陰影縮減 */
.btn:active {
    transform: translateY(2px);
    box-shadow: 
        0 2px 0 0 #2a68a0,
        0 2px 4px rgba(0,0,0,0.1),
        inset 0 1px 3px rgba(0,0,0,0.2);
    background: linear-gradient(180deg, #357abd 0%, #2a68a0 100%);
}

/* 聚焦:無障礙可見焦點環 */
.btn:focus-visible {
    outline: none;
    box-shadow: 
        0 4px 0 0 #2a68a0,
        0 6px 12px rgba(0,0,0,0.15),
        inset 0 1px 0 rgba(255,255,255,0.2),
        0 0 0 3px rgba(74, 144, 226, 0.4);
}

進階:關鍵幂脈動動畫 (呼吸燈效果)

/* 定義關鍵幂 */
@keyframes pulse-glow {
    0%, 100% {
        box-shadow: 
            0 4px 0 0 #2a68a0,
            0 6px 12px rgba(0,0,0,0.15),
            inset 0 1px 0 rgba(255,255,255,0.2),
            0 0 0 0 rgba(74, 144, 226, 0.4); /* 外部光暈從 0 開始 */
    }
    50% {
        box-shadow: 
            0 4px 0 0 #2a68a0,
            0 6px 12px rgba(0,0,0,0.15),
            inset 0 1px 0 rgba(255,255,255,0.2),
            0 0 20px 6px rgba(74, 144, 226, 0.6); /* 外部光暈擴散 */
    }
}

/* 懸停/聚焦時觸發脈動 */
.btn.pulse:hover,
.btn.pulse:focus-visible {
    animation: pulse-glow 2s ease-in-out infinite;
}

/* 減少動畫偏好用戶 */
@media (prefers-reduced-motion: reduce) {
    .btn.pulse:hover,
    .btn.pulse:focus-visible {
        animation: none;
    }
}

完整 HTML 示例

<button class="btn pulse" type="button">立即開始</button>
<a href="#" class="btn pulse" role="button">免費試用</a>
<input type="submit" class="btn pulse" value="提交訂單">

變體:不同風格按鈕系統

:root {
    --btn-radius: 8px;
    --btn-font: 600 16px/1.5 system-ui;
    --btn-padding: 12px 28px;
    --btn-shadow-base: 0 4px 0 0;
    --btn-shadow-spread: 0 6px 12px rgba(0,0,0,0.15);
    --btn-shadow-inner: inset 0 1px 0 rgba(255,255,255,0.2);
}

/* 主要按鈕 */
.btn-primary {
    --btn-bg-from: #4a90e2; --btn-bg-to: #357abd;
    --btn-shadow-color: #2a68a0;
    --btn-glow-color: rgba(74, 144, 226, 0.5);
    background: linear-gradient(180deg, var(--btn-bg-from), var(--btn-bg-to));
    box-shadow: var(--btn-shadow-base) var(--btn-shadow-color), 
                var(--btn-shadow-spread), 
                var(--btn-shadow-inner);
}
.btn-primary:hover { background: linear-gradient(180deg, #5ba0f2, #458ad0); }
.btn-primary:active { background: linear-gradient(180deg, #357abd, #2a68a0); }

/* 次要按鈕 (外框) */
.btn-secondary {
    background: #fff;
    color: #4a90e2;
    border: 2px solid #4a90e2;
    box-shadow: var(--btn-shadow-inner);
}
.btn-secondary:hover {
    background: #f0f7ff;
    box-shadow: 0 4px 12px rgba(74,144,226,0.2), var(--btn-shadow-inner);
}

/* 危險按鈕 */
.btn-danger {
    --btn-bg-from: #e74c3c; --btn-bg-to: #c0392b;
    --btn-shadow-color: #a0281a;
    --btn-glow-color: rgba(231, 76, 60, 0.5);
}

/* 幽靈按鈕 */
.btn-ghost {
    background: transparent;
    color: #4a90e2;
    box-shadow: none;
}
.btn-ghost:hover { background: rgba(74,144,226,0.08); }

性能優化關鍵點

  • 只動 transform/opacity:動畫屬性限於 transform, opacity, filter 觸發合成器線程,避免佈局/重繪
  • will-change 提示:.btn.pulse { will-change: transform, box-shadow; } (用完移除)
  • 避免動畫寬高/左上右下:觸發 Layout + Paint,嚴重掉幀
  • 減少動畫偏好:@media (prefers-reduced-motion: reduce) 禁用非必要動畫
  • GPU 圖層隔離:transform: translateZ(0) 強制提升圖層 (慎用,記憶體成本)

無障礙檢查清單

  • [ ] 語義標籤:<button>、<a role="button">、<input type="submit">
  • [ ] 焦點可見::focus-visible 清晰外框
  • [ ] 觸控目標 ≥ 44×44px (WCAG 2.5.5)
  • [ ] 顏色對比度 ≥ 4.5:1 (正文) / 3:1 (大文本/UI)
  • [ ] aria-disabled="true" + disabled 屬性雙重保障
  • [ ] 載入狀態:aria-busy="true" + 視覺指示器

瀏覽器支持與前綴 (構建工具自動處理)

/* 構建時由 Autoprefixer 根據 browserslist 自動添加 */
.btn {
    -webkit-transition: ...;
    -moz-transition: ...;
    -ms-transition: ...;
    -o-transition: ...;
    transition: ...;
}
@-webkit-keyframes pulse-glow { ... }
@-moz-keyframes pulse-glow { ... }
@keyframes pulse-glow { ... }
copyright © 萬盛學電腦網 all rights reserved