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


先看看效果

 

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

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

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

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

代碼如下:  

 1 <div >
 2     <img src="images/000.jpg" alt=""/>
 3 </div>
 4 
 5 .class1{
 6     position: absolute;
 7     width:100%;
 8     height:100%;
 9     top: 0;
10     left: 0;
11     background-color: #000;
12     opacity: 0.6;
13     filter:alpha(opacity=60);
14 }
15 .class1 img{
16     position: absolute;
17     top:260px;
18     left: 208px;
19 }

 

 

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

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

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

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

代碼如下:  

 1 <div ></div>
 2 
 3 .class2{
 4     position: absolute;
 5     width:170px;
 6     height:190px;
 7     top: 260px;
 8     left: 208px;
 9     box-shadow: rgba(0,0,0,.6) 0  0  0  100vh;
10 }

 

 

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

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

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

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

代碼如下:

 1 <div class="class3"></div>
 2 .class3{
 3       position: absolute;
 4       width:170px;
 5       height:190px;
 6       top: 0;
 7       left: 0;
 8       border-left-width:208px;
 9       border-left-style: solid;
10       border-left-color:rgba(0,0,0,.6);
11       border-right-width:970px;
12       border-right-style: solid;
13       border-right-color:rgba(0,0,0,.6);
14       border-top-width:260px;
15       border-top-style: solid;
16       border-top-color:rgba(0,0,0,.6);
17       border-bottom-width:253px;
18       border-bottom-style: solid;
19       border-bottom-color:rgba(0,0,0,.6);
20 }

 

 

【 方法四:SVG或者canvas 】

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

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

我以SVG為例,代碼如下:  

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

 


免責聲明!

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



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