毛玻璃效果


鏈接:http://www.cnblogs.com/ghost-xyx/p/5677168.html

先上 demo 和 源碼

其實毛玻璃的模糊效果技術上比較簡單,只是用到了 css 濾鏡(filter)中的 blur 屬性。但是要做一個好的毛玻璃效果,需要注意很多細節。



比如我們需要將上圖中頁面中間的文字區域變成毛玻璃效果,首先想到的是給其設置一個透明度,並添加模糊濾鏡:

複製代碼

.content {
    background-color: rgba(0,0,0,0.3);
    -webkit-filter: blur(2px);
    -moz-filter: blur(2px);
    -ms-filter: blur(2px);
    -o-filter: blur(2px);
    filter: blur(2px);    
}

複製代碼

可是生成的效果卻是下面這樣:

從這個失敗的例子我們得到兩個結論:

1. 對元素直接使用模糊會將其內容全部模糊掉,爲了保證文字不會模糊掉需要多一個層單獨應用模糊效果。

2. 模糊效果並不會應用到其背後的元素上,所以需要使用 content 區域有和背景相同的背景圖並進行模糊。

先解決第一個問題:

多一個層級的方法不通過添加元素,而通過僞元素。

複製代碼

.content {
    z-index: 1;
}.content:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255,255,255,0.8);
    z-index: -1;
}

複製代碼

這裏有兩點需要注意,由於僞元素不能通過 width:100% 和 height:100% 來繼承宿主元素的尺寸,所以通過上述方式來繼承 content 的尺寸;爲了使僞元素位於 content 的下面這裏給其設置 z-index:-1,爲不使其隱藏到背景圖的後面,這裏給 content 設置 z-index:1。

效果:

接下來給 content::after 設置相同的背景圖。

如上圖,即使我們設置了相同的 background-postion 與 background-size,中間部分的圖和大背景還是沒有拼接成功。

解決這個問題的方法很簡單,只需要添加 background-p_w_upload: fixed 屬性,之後爲其進行模糊處理。

複製代碼

.content {
    background-position: center top;
    background-size: cover;
}.content::after {
    background-p_w_picpath: url(xxx.jpg);
    background-position: center top;
    background-size: cover;
    background-p_w_upload: fixed;
    -webkit-filter: blur(20px);
    -moz-filter: blur(20px);
    -ms-filter: blur(20px);
    -o-filter: blur(20px);
    filter: blur(20px);
}

複製代碼

可以看到基本得到了我們想要的效果,美中不足的是在元素的邊緣模糊的效果減弱了。爲了解決這個問題,我們將僞元素的範圍擴大一些,同時爲了效果不超出 content 的範圍,給其設置 overflow:hidden 屬性。

.content {  overflow: hidden;
}.content::after {  margin: -30px;
}

這樣一個比較完美的毛玻璃效果就完成了,無論你如何改變瀏覽器窗口的尺寸,content 部分的背景圖都能很好的與背景拼接,這都歸功於 background-p_w_upload 屬性。

附上源碼:

<!doctype html>
<html lang="en">

	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}
			
			html,
			body {
				font-size: 19px;
				font-family: 'Verdana', 'Arial';
				color: rgba(0, 0, 0, 0.8);
				width: 100%;
				height: 100%;
			}
			
			.main {
				width: 100%;
				height: 100%;
				position: relative;
				background: url(img/bg1.jpg) center top;
				background-size: cover;
			}
			
			.content {
				width: 800px;
				height: 400px;
				position: absolute;
				top: 50%;
				left: 50%;
				overflow: hidden;
				margin-top: -200px;
				margin-left: -400px;
				border-radius: 10px;
				box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
				z-index: 100;
				padding: 50px;
				box-sizing: border-box;
				/*不會把盒子撐開*/
			}
			
			.content::before {
				content: "";
				position: absolute;
				top: 0px;
				left: 0px;
				right: 0px;
				bottom: 0px;
				z-index: -1;
				/*-1 可以當背景*/
				-webkit-filter: blur(20px);
				filter: blur(20px);
				margin: -30px;  /*消除邊緣透明*/
				background: url(img/bg1.jpg) center top;
				background-size: cover;
				/*平鋪*/
				background-p_w_upload: fixed;
				/*位置固定*/
			}
			
			.content h1 {
				text-align: center;
				margin-bottom: 20px;
			}
			
			.content p {
				line-height: 1.7;
				/*1.7倍行間距*/
			}
		</style>
	</head>

	<body>
		<div class="main">
			<div class="content">
				<h1>WARFRAME</h1>
				<p>The Grineer, with their endless clone armies, have plunged the system into chaos. There is only one force that can match them, you. You are Tenno, an ancient warrior, a master of gun and blade. You wield the mighty Warframes and command their awesome powers. Forge your weapons. Gather like-minded Tenno and take the fight back to the Grineer. The Origin System needs you once again. Are you ready?</p>
			</div>
		</div>
	</body>

</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章