Css3新特性應用之視覺效果

來源:互聯網
上載者:User
本文介紹了Css3新特性應用之視覺效果,具體有單側陰影、不規則投影、染色體效果、毛半透明效果、折角效果,具體實現如下:

一、單側陰影

1、box-shadow屬性的應用,格式:h-shadow v-shadow blur spread color inset屬性取值介紹 h-sahdow:水平陰影的位置,允許負值

①v-shadow:垂直陰影的位置,允許負值

②blur:模糊距離

③spread:陰影的尺寸,擴張距離,可以為負數

④color:陰影的顏色

⑤inset/outset:內部或者外部陰影

2、陰影的擴張距離對四邊都有效,不能單獨應用於單邊。

3、box-shadow支援多組值同時生效的設定

範例程式碼:

.wrap{            width: 200px;            height: 120px;            background: yellowgreen;            box-shadow: 2px 0px 4px -2px black,                        -2px 0px 4px -2px black;        }

二 、不規則投影

1、利用border-radius產生的形狀,用投影很好,但是如果加入了虛擬元素和半透明的裝飾,陰影表現就很不好了,如下情況都會有問題。

① 半透明映像、背景映像、或者border-image

②元素設定了點狀、虛線或半透明的邊框,但沒有背景(或者background-clip不是border-box時)

③元素內部有小角是用虛擬元素產生

④通過clip-path產生的形狀

解決辦法:利用svg的drop-shadow來實現

範例程式碼:

.wrap{            width: 200px;            height: 120px;            border: 6px dotted yellowgreen;            --box-shadow: 0px 0px 4px 0px black;            -webkit-filter: drop-shadow(2px 0px 2px rgba(0,0,0,1))        }

三、染色體效果

1、基於濾鏡實現,應用filter屬性的相關值,調整圖片的飽合度、亮度等值

2、基於min-blend-mode實現,作用:實現元素內容與背景以及下面的元素髮生“混合”

3、基本background-blend-mode實現,作用:實現背景顏色與背景圖片、背景圖片與圖片的混合

三種情況的範例程式碼:

.wrap1{            width: 200px;            height: 120px;            overflow: hidden;        }        .wrap1 > img{            max-height: 100%;            max-width: 100%;            -webkit-filter: sepia(1) saturate(4) hue-rotate(150deg);        }        .wrap2{            width: 200px;            height: 120px;            background: hsl(335, 100%, 50%);            overflow: hidden;        }        .wrap2 > img{            height: 100%;            width: 100%;            mix-blend-mode: luminosity;        }        .wrap3{            width: 200px;            height: 120px;            background-size: cover;            background-color: hsl(335, 100%, 50%);            background-image: url("../img/cat.png");             background-blend-mode: luminosity;        }

四、毛半透明效果

主要實現原理:內容虛擬元素背景與底層背景相同的圖片;並加上filter:blur模糊濾鏡即可。注意blur不能應用在底層背景,也不能應用在元素的背景上(這樣會地元素本身應用blur模糊,會導致文本看不見),只能就用在虛擬元素上。

代碼如下:

body{    background: url("../img/cat.png") no-repeat;    background-size: cover;}.wrap{    position: relative;    width: 500px;    margin: 0px auto;    padding: 10px;    line-height: 1.5;    background: hsla(0, 0%, 100%, .3);    overflow: hidden;}.wrap::before{    content: '';    background: url("../img/cat.png") 0/cover fixed;    position: absolute;    top: 0; right: 0; bottom: 0; left: 0;    filter: blur(20px);    z-index: -1;    margin: -30px;}

代碼說明:1、body與wrap虛擬元素都應用相同的背景圖片

2、wrap的background-attachment設定為fixed,讓背景圖不要跟隨滾動一起動

3、wrap虛擬元素設定為絕對位置,且z-index層級只高於背景

4、利用blur設定wrap虛擬元素的模糊尺寸

5、用margin負值增加寬度,父元素用overflow:hidden隱藏溢出,讓模糊背景更加真實。

五、折角效果

實現步驟

1、首先利用linear-gradient實現切角效果2、然後再利用linear-gradinet產生一個三角形,並設定他的位置與寬高

代碼如下:

.wrap{        background: linear-gradient(to left bottom, transparent 50%, rgba(0, 0, 0, .4) 0) no-repeat 100% 0/2em 2em,        linear-gradient(-135deg, transparent 1.4em, #58a 0);        width: 200px;        height: 120px;    }

注意

1、100% 0/2em 2em在定位背景元素的位置與寬高,尤其是2em的寬與高都是背景元素正常的寬度。

2、而第二個linear-gradient中的1.4em是沿著漸層軸進行度量的,也就是漸層軸到元素頂邊的距離,本例是漸層軸到右上邊頂的距離

3、to left bottom是表示漸層從左下角開始

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援topic.alibabacloud.com。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.