CSS 的 filter 是一組以函式方式呼叫的視覺效果屬性,能在不改動圖檔的前提下,為元素加上透明、模糊、陰影、翻轉、灰階等處理。以下整理基本語法與各濾鏡的參數含義。
style="filter: filtername(parameter1, parameter2, ...)"
其中 filtername 為濾鏡名稱,後方括號內依序填入該濾鏡的參數。
| 濾鏡 | 效果 |
|---|---|
| Alpha | 設定透明度層次 |
| blur | 建立高速移動的模糊效果 |
| Chroma | 將指定顏色處理為透明 |
| DropShadow | 建立固定方向的物件陰影 |
| FlipH | 水平鏡像 |
| FlipV | 垂直鏡像 |
| glow | 在物件邊緣外加上光暈 |
| gray | 將圖片轉為灰階 |
| invert | 色彩反轉 |
| light | 在物件上建立光源 |
| mask | 在物件上建立透明遮罩 |
| shadow | 建立偏移的固定陰影 |
| wave | 波紋扭曲效果 |
| Xray | 使物件呈現如 X 光照射般的輪廓 |
style="filter:Alpha(Opacity=opacity, FinishOpacity=finishopacity,
Style=style, StartX=startX, StartY=startY,
FinishX=finishX, FinishY=finishY)"
filter:Alpha(Opacity="0",FinishOpacity="75",Style="2")
style="filter:Blur(Add=add, Direction=direction, Strength=strength)"
filter:Blur(Add="1",Direction="45",Strength="5")
style="filter:Chroma(Color=color)"
color:採 #rrggbb 格式,可任意指定。
filter:Chroma(Color="#FFFFFF")
style="filter:DropShadow(Color=color, OffX=offX, OffY=offY, Positive=positive)"
#rrggbb 格式。filter:DropShadow(Color="#6699CC",OffX="5",OffY="5",Positive="1")
filter:FlipH /* 水平鏡像 */
filter:FlipV /* 垂直鏡像 */
style="filter:Glow(Color=color, Strength=strength)"
filter:Glow(Color="#6699CC",Strength="5")
filter:Gray /* 灰階化 */
filter:Invert /* 反色 */
filter:Xray /* X 光透視效果 */
style="filter:Mask(Color=color)"
filter:Mask(Color="#FFFFE0")
filter:Shadow(Color=color, Direction=direction)
#rrggbb 格式。filter:Shadow(Color="#6699CC", Direction="135")
filter:Wave(Add=add, Freq=freq, LightStrength=strength,
Phase=phase, Strength=strength)
filter:wave(Add="0", Phase="4", Freq="5", LightStrength="5", Strength="2")
filter: progid:DXImageTransform.Microsoft.Gradient(
GradientType=0, StartColorStr='#B5CCFA', EndColorStr='#B5CCFA');
這套 filter 屬於早期 IE 的私有實作,僅在 IE 系列(尤其 IE4~IE8)中生效,其他瀏覽器會直接忽略。若要在跨瀏覽器環境中達到相同視覺效果,建議改用標準屬性:
/* 灰階 */
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
/* 模糊 */
-webkit-filter: blur(5px);
filter: blur(5px);
/* 透明 */
opacity: 0.5;
/* 鏡像 */
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
只有在需要維護老舊系統、或必須支援 IE 環境的專案中,這份 filter 對照表才有實際用武之地。新專案請優先採用標準 CSS 濾鏡。