A variety of methods to realize the hollowing effect of mask layer

Let's see the effect first

 

[method 1: screenshot simulation]

Principle: first cut a picture at the same position, create a mask layer, and then locate the picture at the corresponding position. The

Advantages: simple principle; Good compatibility, compatible with IE6 and IE7; Multiple hollows can be realized at the same time. The

Disadvantages: this method is only suitable for static pages, not for scrollable pages. It is also not suitable for pages whose content will change. The

The codes are as follows:

<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;
}

 

[method 2: CSS3 shadow attribute implementation]

Principle: use the shadow attribute of CSS3. The

Advantages: easy to realize; It is suitable for any page and will not be limited by the page.

Disadvantages: the compatibility is not very good. It can only be compatible with IE9. The

The codes are as follows:

<div ></div>

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

 

[method 3: CSS border attribute implementation]

Principle: use border attributes. First locate an empty box in the target area, and then fill it with a border around it. The

Advantages: easy implementation, good compatibility, and compatibility to IE6 and IE7; It is suitable for any page and will not be limited by the page.

Disadvantages: the process of implementing compatibility is relatively complex. The

The codes are as follows:

<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);
}

 

[method 4: SVG or canvas]

Principle: use the drawing function of SVG or canvas. The

Advantages: it can hollow out multiple at the same time. Disadvantages: poor compatibility and relatively complex implementation process. The

I take SVG as an example, and the code is as follows:

<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>

Tags: html

Posted by redrabbit on Wed, 01 Jun 2022 21:42:23 +0530