CSS使用filter和backdrop-filter實(shí)現(xiàn)高斯模糊效果(示例代碼)
背景
今天接到一個需求是,使用高斯模糊的效果對一個頁面進(jìn)行模糊處理,正好借這個機(jī)會來整理一下 css3 中高斯模糊的兩個 API
API介紹
filter
說明:
該 API 是一個過濾器,不僅能實(shí)現(xiàn)高斯模糊,還有很多比如顏色偏移、飽和度、灰度等等
語法:
// 使用空格分隔多個濾鏡 filter: none; // 高斯模糊 filter: blur(4px); // 線性亮度 filter: brightness(); // 對比度 filter: contrast(); // 陰影效果 filter: drop-shadow(); // 灰度 filter: grayscale(); // 色相旋轉(zhuǎn) filter: hue-rotate(); // 反轉(zhuǎn)圖像 filter: invert(); // 轉(zhuǎn)換透明度 filter: opacity(); // 飽和度 filter: saturate(); // 褐色 filter: sepia(); // SVG濾鏡 filter: url();
其中高斯模糊 filter: blur();
backdrop-filter
說明:
當(dāng)你創(chuàng)造一個元素加上這個屬性后,會使得這個元素后面的區(qū)域添加效果(如模糊或顏色偏移)
對比:
filter 屬性必須要加載圖像上或者背景圖上,而 backdrop-filter 只要是一個元素就可以。
語法:
backdrop-filter: blur(2px); backdrop-filter: brightness(60%); backdrop-filter: contrast(40%); backdrop-filter: drop-shadow(4px 4px 10px blue); backdrop-filter: grayscale(30%); backdrop-filter: hue-rotate(120deg); backdrop-filter: invert(70%); backdrop-filter: opacity(20%); backdrop-filter: sepia(90%); backdrop-filter: saturate(80%);
示例
filter例一
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.wrapBox2 {
width: 800px;
height: 300px;
overflow: hidden;
position: relative;
background-image: url("./win.jpeg");
background-size: 100% 100%;
background-repeat: no-repeat;
filter: blur(10px);
}
.subBox {
position: absolute;
width: calc(100% - 100px);
height: calc(100% - 100px);
z-index: 2;
}
.text {
position: relative;
/* z-index: 10; */
font-size: 40px;
font-weight: bold;
color: #f00;
}
</style>
</head>
<body>
<div class="wrapBox2">
<div class="subBox"></div>
<div class="text">全部模糊</div>
</div>
</body>
</html>
這里要注意的一點(diǎn)是,添加模糊后,實(shí)際的大小會超出我們設(shè)置的寬高,因?yàn)橹車拿呅Ч?,你可以在外面包一層并設(shè)置 overflow: hidden;
filter例二
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.wrapBox2 {
width: 800px;
height: 300px;
/* overflow: hidden; */
position: relative;
}
.subBox {
width: 100%;
height: 100%;
position: absolute;
width: calc(100% - 100px);
height: calc(100% - 100px);
z-index: 2;
filter: blur(10px);
}
.text {
position: relative;
/* z-index: 10; */
font-size: 40px;
font-weight: bold;
color: #f00;
}
</style>
</head>
<body>
<div class="wrapBox2">
<img src="./win.jpeg" class="subBox" />
<div class="text">全部模糊</div>
</div>
</body>
</html>
這種方式的話,文字和圖片由于是平級的,所以文字要么在圖片下方,要么在上方(根據(jù)z-index來控制),而不會對文字進(jìn)行模糊。
backdrop-filter例一
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.wrapBox2 {
width: 800px;
height: 300px;
overflow: hidden;
position: relative;
background-image: url("./win.jpeg");
background-size: 100% 100%;
background-repeat: no-repeat;
}
.subBox {
position: absolute;
width: calc(100% - 100px);
height: calc(100% - 100px);
z-index: 2;
backdrop-filter: blur(10px);
/* top: 100px; */
}
.text {
position: relative;
/* z-index: 10; */
font-size: 40px;
font-weight: bold;
color: #f00;
}
</style>
</head>
<body>
<div class="wrapBox2">
<div class="subBox"></div>
<div class="text">部分模糊</div>
</div>
</body>
</html>
可以看到,backdrop-filter 屬性不必設(shè)置在一個圖片元素上面,而是任何元素上面就行,這種方式我覺得更加靈活
總結(jié)
- 如果只是模糊一張圖片,那么直接用 filter 就可以實(shí)現(xiàn)。
- 如果想要用一個模糊蒙層蓋住html頁面/圖片的某一部分,那么使用 backdrop-filter。
當(dāng)然,使用 backdrop-filter 也可以滿足第一種場景。
到此這篇關(guān)于CSS使用filter和backdrop-filter實(shí)現(xiàn)高斯模糊效果(示例代碼)的文章就介紹到這了,更多相關(guān)css高斯模糊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
CSS filter:hue-rotate色調(diào)旋轉(zhuǎn)濾鏡實(shí)現(xiàn)按鈕批量生產(chǎn)
這篇文章主要介紹了CSS filter:hue-rotate色調(diào)旋轉(zhuǎn)濾鏡實(shí)現(xiàn)按鈕批量生產(chǎn)的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-11-18
這篇文章主要介紹了CSS中filter屬性的使用詳解,css3中的filter屬性可以說是簡單易用且強(qiáng)大,filter 屬性定義了元素的可視效果,非常具有實(shí)用價值,需要的朋友可以參考下2018-10-15
filter 屬性定義了元素(通常是)的可視效果(例如:模糊與飽和度)。接下來通過本文給大家介紹微信小程序 CSS filter(濾鏡)的使用示例,感興趣的朋友一起看看吧2018-07-06css+filter實(shí)現(xiàn)簡單的圖片透明效果
本文給大家介紹的是CSS結(jié)合filter實(shí)現(xiàn)簡單的圖片透明效果的實(shí)例,主要使用filter的功能對圖片元素進(jìn)行透明度控制,需要的朋友可以參考下2015-01-13
css中filter屬性和backdrop-filter的應(yīng)用與區(qū)別詳解
這篇文章主要介紹了css中filter屬性和backdrop-filter的應(yīng)用與區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面2020-09-14



