1. 程式人生 > 實用技巧 >遮罩層鏤空效果的多種實現方法

遮罩層鏤空效果的多種實現方法

先看看效果

【 方法一:截圖模擬實現 】

原理:先截一張相同位置的圖片,建立一個遮罩層,然後把圖片定位在相應的位置上。

優點:原理簡單;相容性好,可以相容到IE6、IE7;可以同時實現鏤空多個。

缺點:此方法只適合靜止頁面,不適合可以滾動的頁面。也不適合頁面內容會發生變換的頁面。

程式碼如下:

<div >
    <img src="images/000.jpg" alt=""/>
</div>

.class1{
    position: absolute;
    width:100%;
    height:100%;
    top: 0;
    left: 0;
    background-color: #000;
    opacity: 0.6;
    filter:alpha(opacity=60);
}
.class1 img{
    position: absolute;
    top:260px;
    left: 208px;
}

【方法二:css3陰影屬性實現 】

原理:利用css3的陰影屬性。

優點:實現方便;適合任何頁面,不會受頁面的限制。

缺點:相容不太好,只能相容到IE9。

程式碼如下:

<div ></div>

.class2{
    position: absolute;
    width:170px;
    height:190px;
    top: 260px;
    left: 208px;
    box-shadow: rgba(0,0,0,.6) 0  0  0  100vh;
}

【 方法三:CSS邊框屬性實現】

原理:利用邊框屬性。先將一個空盒子定位在目標區域,然後在其四周用邊框填充。

優點:實現方便,相容性好,可以相容到IE6、IE7;適合任何頁面,不會受頁面的限制。

缺點:要做相容實現過程則相對複雜。

程式碼如下:

<div class="class3"></div>
.class3{
      position: absolute;
      width:170px;
      height:190px;
      top: 0;
      left: 0;
      border-left-width:208px;
      border-left-style: solid;
      border-left-color:rgba(0,0,0,.6);
      border-right-width:970px;
      border-right-style: solid;
      border-right-color:rgba(0,0,0,.6);
      border-top-width:260px;
      border-top-style: solid;
      border-top-color:rgba(0,0,0,.6);
      border-bottom-width:253px;
      border-bottom-style: solid;
      border-bottom-color:rgba(0,0,0,.6);
}

廣州vi設計公司 http://www.maiqicn.com 我的007辦公資源網 https://www.wode007.com

【 方法四:SVG或者canvas】

原理:利用SVG或者canvas的繪圖功能。

優點:可以同時鏤空多個。 缺點:相容性不好,實現過程相對複雜。

我以SVG為例,程式碼如下:

<svg style="position: absolute;" width="1366" height="700">
    <defs>
        <mask id="myMask">
            <rect x="0" y="0" width="100%" height="100%" style="stroke:none; fill: #ccc"></rect>
            <rect id="circle1" width="170" height="190" x='208' y="260" style="fill: #000" />
        </mask>
    </defs>
    <rect x="0" y="0" width="100%" height="100%" style="stroke: none; fill: rgba(0, 0, 0, 0.6); mask: url(#myMask)"></rect>
</svg>