mask


mask

一、現狀

  • Firefox,Chrome以及移動端等都已經全線支持,尤其Firefox瀏覽器,從版本53開始,已經全面支持了CSS3 mask屬性.

二、Mask 的原理

  • 蒙板可以是 CSS3 漸變或者半透明的PNG圖片,蒙板元素的alpha值為0的時候會覆蓋下面的元素,為1的時候會完全顯示下面的內容。如下:

三、mask屬性

  • mask屬性實際上已經成為了諸多mask-*的縮寫,這和background, border性質是一樣的。

3.1.mask-image

mask-image指遮罩使用的圖片資源,默認值是none,也就是無遮罩圖片。因此,和border屬性中的border-style屬性類似,是一個想要有效果就必須設定的屬性值。

mask-image遮罩所支持的圖片類型非常的廣泛,可以是url()靜態圖片資源,格式包括JPG,PNG以及SVG等都是支持的;也可以是動態生成的圖片,例如使用各種CSS3漸變繪制的圖片,同時還支持多背景,因此理論上,使用mask-image我們可以遮罩出任意我們想要的圖形,非常強大。

/* 圖片資源 */
-webkit-mask-image: url(mask.png);
/* css的漸變屬性 */
 -webkit-mask-image: linear-gradient(to bottom, rgba(0,0,0,1), rgba(0,0,0,0)); 

3.2.mask-repeat

默認值為repeat,可選值與background-repeat相同。

/* 單個值 */
-webkit-mask-repeat: no-repeat | repeat-x | repeat-y | space | round | repeat(默認值);
/* 兩個值 horizontal(水平)  vertical(垂直) (以下僅為示例)*/
-webkit-mask-repeat: repeat space | repeat repeat | round space | no-repeat round;

/* Multiple values */
-webkit-mask-repeat: space round, no-repeat;
-webkit-mask-repeat: round repeat, space, repeat-x;

/* 多層遮罩 */
mask-image: url('mask1.png'), url('mask2.png');
mask-repeat: repeat-x, repeat-y;

round:當背景圖像不能以整數次平鋪時,會根據情況縮放圖像。(CSS3)

space:當背景圖像不能以整數次平鋪時,會用空白間隙填充在圖像周圍。(CSS3)

3.3.mask-position

默認值為0 0,可選值與background-position相同。

mask-position: top;
mask-position: bottom;
mask-position: left;
mask-position: right;
mask-position: center;
mask-position: 30% 50%;
mask-position: 10px 5rem;

由於mask-image支持多遮罩圖片,因此,mask-position也支持多屬性值,例如:

mask-position: 0 0, center;

3.4.mask-clip

默認值為border-box,可選值與background-clip相同。

-webkit-mask-clip: content-box | padding-box | border-box | margin-box | fill-box | stroke-box | view-box | no-clip;

-webkit-mask-clip: border-box;
-webkit-mask-clip: padding-box;
-webkit-mask-clip: content-box;
-webkit-mask-clip: text;

/* Multiple values */
-webkit-mask-clip: padding-box, no-clip;
-webkit-mask-clip: view-box, fill-box, border-box;

3.5.mask-origin

默認值為border-box,可選值與background-origin相同。

/* Keyword values */
mask-origin: content-box;
mask-origin: padding-box;
mask-origin: border-box;
mask-origin: margin-box;
mask-origin: fill-box;
mask-origin: stroke-box;
mask-origin: view-box;

/* Multiple values */
mask-origin: padding-box, content-box;
mask-origin: view-box, fill-box, border-box;

/* Non-standard keyword values */ 
-webkit-mask-origin: content; 
-webkit-mask-origin: padding; 
-webkit-mask-origin: border;

3.6.【mask-size】

默認值為auto,可選值與background-size相同。

.divTest{
    padding: 40px;
    background-color: #03ff36;
    background-image: url(http://cdn01.baidu-img.cn/timg?image_search&quality=80&size=b10000_10000&sec=1470644878&di=f054ab9449175833cf84b3429acacd88&imgtype=jpg&src=http%3A%2F%2Fimg0.bdstatic.com%2Fimg%2Fimage%2Fshouye%2Fxiaoxiao%2Fis099s8ky-PC.jpg));
    -webkit-mask-image: url(http://image27.360doc.com/DownloadImg/2011/04/2015/11077777_58.png)), url(http://img.t.sinajs.cn/t6/style/images/global_nav/WB_logo.png));
    -webkit-mask-repeat: space,repeat-y;
    -webkit-mask-clip: text; 
    -webkit-mask-size: 5%, 8%;
    border: 30px solid red;
}

四、實戰

4.1.圖形蒙版

  • 簡單使用
<style>
    img{
        -webkit-mask-image: url(img/mask1.png);
        -webkit-mask-repeat:no-repeat;
        -webkit-mask-position: center;
    }
</style>
<img src="img/origin1.jpeg" alt="">
  • 效果

  • mask 支持 svg, png 還可以用 gif,如cdepen上有人用mask 做了個海綿寶寶動畫。【查看demo】

4.2.漸變蒙版

 -webkit-mask-image: -webkit-gradient(linear,left top,left bottom,from(rgba(0,0,0,1)),to(rgba(0,0,0,0)));
  • 效果

4.3.蒙版位置

  • 4.1基礎上,改變-webkit-mask-position: center;
  • 作用:可用於app使用引導。
(function(window){
    var img = document.getElementById('img'),
        isIn = false;
    img.onmouseover = function(e){
        isIn = true;
    }

    img.onmousemove = function(e){
        if(isIn){
            img.style.cssText = '-webkit-mask-position:'+(e.pageX-150)+'px '+(e.pageY-123)+'px;'
        }
    }
    img.onmouseout = function(e){
        isIn = false;
    }
})(window)

4.4.蒙版動畫

4.5.蒙版動畫-轉場

HTML結構:

<div class="sec1"></div>
<div class="sec2"></div>

CSS:

* {margin: 0;padding: 0;}
html,body{width: 100%;height: 100%;overflow: hidden;}
.sec1,.sec2{
    width: 100%;
    height:100%;
}
.sec1 {
    background: url(img/bg2.jpg) center top no-repeat;
    position: absolute;
    left:0;
    top: 0;
}

.sec2 {
    background: url(img/bg3.jpg) center top no-repeat;
}
/**center**/
.sec1 {
    -webkit-mask-image: url(img/mask-center.png);
    -webkit-mask-size: 3000% 100%;
    -webkit-mask-position: 0 center;
    animation: startMask 9.8s steps(29) both 1;
}
@keyframes startMask {
    to {
        -webkit-mask-position: 100% center;
        mask-position: 100% center
    }
}
/**top**/
.sec1 {
    -webkit-mask-image: url(img/mask-left-top.png);
    -webkit-mask-size:100% 3000% ;
    -webkit-mask-position:0 0;
    animation: startMask 9.8s steps(29) both 1;
}

@keyframes startMask {
    to {
        -webkit-mask-position:0 100% ;
        mask-position: 0 100%
    }
}
/**down**/
.sec1 {
    -webkit-mask-image: url(img/mask-down.png);
    -webkit-mask-size:4000% 100% ;
    -webkit-mask-position: 0 center;
    animation: startMask 9.8s steps(39) both 1;
}

@keyframes startMask {
    to {
        -webkit-mask-position: 100% center;
        mask-position:  100% center
    }
}
/**left**/
.sec1 {
    -webkit-mask-image: url(img/mask-left-top.png);
    -webkit-mask-size:100% 3000% ;
    -webkit-mask-position:0 0;
    animation: startMask 9.8s steps(29) both 1;
}

@keyframes startMask {
    to {
        -webkit-mask-position:0 100% ;
        mask-position: 0 100%
    }
}

4.6.性能優化

<style>
	.original-mask{ -webkit-mask: url(img/original-mask.png) no-repeat;}
</style>
<img src="img/original-jpg.jpg" class="original-mask">

【demo】


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM