萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

HTML5 文字發光脈動特效:CSS3 Animation 替代舊式 Filter 實現

效果回顧與現代化

原始代碼使用 IE 專屬 filter: Glow() + setTimeout 輪詢實現文字發光脈動。現代瀏覽器已全面支援 CSS3 text-shadow + @keyframes,效能更佳、程式碼更簡潔、無需 JavaScript。

現代標準實作(純 CSS)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>文字發光脈動特效</title>
    <style>
        .pulse-text {
            font: bold 48px/1.2 'Microsoft JhengHei', sans-serif;
            color: #fff;
            text-align: center;
            padding: 2rem;
            background: #1a1a1a;
            /* 核心動畫 */
            animation: pulseGlow 2s ease-in-out infinite alternate;
        }
        
        @keyframes pulseGlow {
            0% {
                text-shadow: 
                    0 0 5px #0f0,   /* 內層亮光 */
                    0 0 10px #0f0,  /* 中層擴散 */
                    0 0 20px #0f0,  /* 外層暈影 */
                    0 0 40px #0f0;  /* 遠層光暈 */
            }
            100% {
                text-shadow: 
                    0 0 2px #0f0,
                    0 0 5px #0f0,
                    0 0 10px #0f0,
                    0 0 15px #0f0;
            }
        }
        
        /* 多色脈動變體 */
        .pulse-multi {
            font: bold 36px/1.5 sans-serif;
            color: #fff;
            animation: pulseMulti 3s ease-in-out infinite;
        }
        
        @keyframes pulseMulti {
            0%, 100% { 
                text-shadow: 0 0 10px #f0f, 0 0 20px #f0f, 0 0 40px #f0f; 
            }
            25% { 
                text-shadow: 0 0 10px #0ff, 0 0 20px #0ff, 0 0 40px #0ff; 
            }
            50% { 
                text-shadow: 0 0 10px #ff0, 0 0 20px #ff0, 0 0 40px #ff0; 
            }
            75% { 
                text-shadow: 0 0 10px #f0f, 0 0 20px #f0f, 0 0 40px #f0f; 
            }
        }
        
        /* 文字描邊 + 發光結合 */
        .pulse-stroke {
            font: 900 60px/1 'Impact', sans-serif;
            color: transparent;
            -webkit-text-stroke: 2px #fff;
            text-stroke: 2px #fff;
            animation: pulseStroke 1.5s ease-in-out infinite alternate;
        }
        
        @keyframes pulseStroke {
            0% { 
                filter: drop-shadow(0 0 3px #0ff) drop-shadow(0 0 6px #0ff); 
            }
            100% { 
                filter: drop-shadow(0 0 10px #f0f) drop-shadow(0 0 20px #f0f); 
            }
        }
    </style>
</head>
<body style="background: #111; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3rem; padding: 2rem;">
    <div class="pulse-text">Welcome To You!!</div>
    <div class="pulse-multi">多色脈動發光效果</div>
    <div class="pulse-stroke">描邊發光</div>
</body>
</html>

CSS 發光核心技巧

text-shadow 多層疊加

text-shadow: 
    0 0 2px   #0f0,  /* 核心高光 */
    0 0 5px   #0f0,  /* 擴散層 */
    0 0 15px  #0f0,  /* 暈影層 */
    0 0 30px  #0f0;  /* 光暈層 */

多組陰影以逗號分隔,瀏覽器由內而外疊加。模糊半徑遞增、顏色可漸變。

filter: drop-shadow() 替代方案

對於描邊文字(color: transparent; text-stroke),text-shadow 不生效,改用:

filter: drop-shadow(0 0 5px #0ff) drop-shadow(0 0 15px #0ff);

drop-shadow 遵循元素實際形狀(含透明區域),適合複雜形狀發光。

JavaScript 控制版(需動態互動時)

<style>
    .js-pulse {
        font: bold 40px sans-serif;
        color: #fff;
        text-align: center;
        transition: text-shadow 0.5s ease; /* 平滑過渡 */
    }
</style>

<div class="js-pulse" id="pulseText">互動式脈動文字</div>
<button id="toggleBtn">暫停/繼續</button>
<input type="range" id="speed" min="0.5" max="5" step="0.1" value="2"> 速度
<input type="color" id="color" value="#00ff00"> 顏色

<script>
const text = document.getElementById('pulseText');
const btn = document.getElementById('toggleBtn');
const speedInput = document.getElementById('speed');
const colorInput = document.getElementById('color');

let animating = true;
let animationId;

function startPulse() {
    let intensity = 0;
    let direction = 1;
    const baseColor = colorInput.value;
    
    function frame() {
        if (!animating) return;
        intensity += direction * 0.02;
        if (intensity >= 1) { intensity = 1; direction = -1; }
        if (intensity <= 0) { intensity = 0; direction = 1; }
        
        const blur = 5 + intensity * 30;
        const spread = 2 + intensity * 15;
        text.style.textShadow = `
            0 0 ${blur}px ${baseColor},
            0 0 ${spread}px ${baseColor}
        `;
        animationId = requestAnimationFrame(frame);
    }
    frame();
}

function stopPulse() {
    cancelAnimationFrame(animationId);
    text.style.textShadow = 'none';
}

btn.addEventListener('click', () => {
    animating = !animating;
    btn.textContent = animating ? '暫停' : '繼續';
    if (animating) startPulse(); else stopPulse();
});

speedInput.addEventListener('input', () => {
    // 速度控制需重啟動畫或用 CSS animation-duration
    text.style.animationDuration = (2 / speedInput.value) + 's';
});

colorInput.addEventListener('input', () => {
    // 即時更新顏色
    if (!animating) text.style.textShadow = 'none';
});

startPulse();
</script>

原始 IE Filter 代碼遺留問題

問題說明
瀏覽器相容filter: Glow() 僅 IE 4-10 支援,Edge/Chrome/Firefox/Safari 全無效
效能setTimeout 字串參數 "showme()" 等同 eval,極不安全
變數污染大量全域變數 span, timer1, i, j 污染全域命名空間
FP 標記污染原始碼混入 <fp class='...'> 編輯器垃圾標記,導致語法錯誤
無無障礙閃爍動畫可能誘發光敏性癲癇,缺乏 prefers-reduced-motion 尊重

無障礙優化(必要)

@media (prefers-reduced-motion: reduce) {
    .pulse-text, .pulse-multi, .pulse-stroke, .js-pulse {
        animation: none !important;
        text-shadow: 0 0 5px currentColor !important; /* 靜態微光 */
    }
}

/* 降低對比度模式 */
@media (prefers-contrast: high) {
    .pulse-text { color: #ff0; text-shadow: 0 0 3px #ff0; }
}

應用場景建議

  • Hero 區塊標題強調
  • 通知/警示訊息吸引注意
  • 遊戲 UI:血量警告、技能就緒提示
  • 資料儀表板:關鍵指標異常閃爍
  • 節日主題頁面:聖誕、新年、萬聖節氛圍
copyright © 萬盛學電腦網 all rights reserved