css3製作毛半透明效果的範例程式碼詳解

來源:互聯網
上載者:User
忙裡偷閒,最近又在看許多比較酷炫的效果。現在基於jquery的外掛程式比較多,但是很多外掛程式的相容性不是太好,所以原生的才是王道。在日常當中,毛玻璃已經不常見了,那是一個很久遠年代的東西了。諾,下面就是毛玻璃:

哈哈,不閑扯了,接下來不如正題吧,先看一下終極效果:

嗯,好了,,開始我們的步驟吧:

第一步:進行頁面的基本搭建:

我在body上設定了一張大大的背景圖,然後中間部分是一個p,html代碼如下:

<body><p >    iPhone 7 dramatically improves the most important aspects of the iPhone experience. It introduces advanced new camera systems. The best performance and battery life ever in an iPhone. Immersive stereo speakers. The brightest, most colorful iPhone display. Splash and water resistance.1 And it looks every bit as powerful as it is. This is iPhone 7.</p></body>

  文字多是為了撐開p使其效果更明顯

css的代碼如下:

   body {            min-height: 100vh;            box-sizing: border-box;            margin: 0;            padding-top: calc(50vh - 6em);            font: 150%/1.6 serif;            background: url("http://www.jackzxl.net/wp-content/MyFile/2016/09/iphone.jpg") fixed 0 center;            background-size: cover;        }        p {            margin: 0 auto;            padding: 1em;            max-width: 30em;            border-radius: 0.3em;            box-shadow: 0 0 0 1px hsla(0,0%,100%,.3) inset,            0 .5em 1em rgba(0, 0, 0, 0.6);            text-shadow: 0 1px 1px hsla(0,0%,100%,.3);            background: hsla(0,0%,100%,.3);        }

看上面的樣式代碼,body裡面,vh為視口大小,100為100%,background設定的為固定位置,鋪滿整個元素;在p當中,置中設定背景色然後設定其盒子樣式;然後是這個樣子滴:

 

亮堂堂的小透明玻璃就這麼出來了,那麼開始我們的毛玻璃加工吧

第二步:背景模糊的毛玻璃設定

在css當中,有同一個關於模糊度的設定,為

filter: blur(20px);

後面的數值為模糊度的大小,數值越大越模糊啊,但是目前只支援px,不支持度百分比;

我們直接給p加這個樣式是不行的,因為這樣會連子都變得模糊,這個時候我們可以利用虛擬元素,即::before;

我們使用虛擬元素之前,需要給p加一個相對定位,由於虛擬元素使用模糊度以後會溢出整個p盒子,為了美觀漂亮大氣上檔次,我們需要給p加overflow:hidden;即:


  overflow: hidden;  position: relative;

 p的虛擬元素:

   p::before{            content: '';            position: absolute;            top: 0; right: 0; bottom: 0; left: 0;            z-index: -1;            filter: blur(20px);            margin: -20px;            background: url("http://www.jackzxl.net/wp-content/MyFile/2016/09/iphone.jpg") fixed 0 center;            background-size: cover;        }

  在上面的css代碼中我們可以看出,設定的模糊度,和p是重疊的,背景圖也是和body一樣的,最終效果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.