想象一下,我们有两层背景。
底层是绿色的<div class="green"></div>。为简单起见,我们假设它只是一种颜色。但是在我的项目中,这一层包含一个css animation。
在它上面还有另一层蓝色<div class="blue"></div>。
现在,我想div在两者之上添加另一个,它显示绿色背景(我项目中的动画层)。
我能想到的最接近的例子是,如果你想象一个聚光灯。一切看起来都是黑色的,聚光灯四处移动并显露出背景。
基本上,这就是我所拥有的:
<div class="green">
<div class="blue">
<div class="reveal"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
它看起来像这样。请记住,绿色层是我项目中的动画。
问题:我怎样才能完成.reveal样式来实现上述行为。
.green背景(动画).blue在它上面绘制背景First div绘制的任何内容注意:100%可用宽度和高度的第一个和第二个 div 覆盖。
.green {
background-color: #159c82;
width: 100vw;
height: 100vh;
}
.blue {
background-color: #1b4287;
// I could change this to a sibling div and use,
// position: absolute; but that seems unnecessary
width: 100%;
height: 100%;
}
.reveal {
margin-top: 10px;
margin-left: 10px;
width: 200px;
height: 50px;
// not sure what else to put here to make it work
}
<div class="green">
<div class="blue">
<div class="reveal"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
PS 我发现有一种方法我根本不喜欢。
Tem*_*fif 13
使用掩码创建一个洞,不需要显示 div。您可以稍后更改大小和位置以获得所需的动画:
.green {
background: linear-gradient(45deg,#159c82,red);
height: 100vh;
}
.blue {
background:#1b4287;
height: 100%;
-webkit-mask:
/* you adjust this */
linear-gradient(#fff,#fff)
50px 50px/ /*left top*/
200px 20px, /*width height*/
/**/
linear-gradient(#fff,#fff);
-webkit-mask-repeat:no-repeat;
-webkit-mask-composite: destination-out;
mask:
/* you adjust this */
linear-gradient(#fff,#fff)
50px 50px/ /*left top*/
200px 20px, /*width height*/
/**/
linear-gradient(#fff,#fff);
mask-repeat:no-repeat;
mask-composite:exclude;
transition:.5s;
}
.blue:hover {
-webkit-mask-position:100px 100px,0 0;
mask-position:100px 150px,0 0;
-webkit-mask-size:300px 50px,auto;
mask-size:300px 50px,auto;
}
body {
margin: 0;
}Run Code Online (Sandbox Code Playgroud)
<div class="green">
<div class="blue">
</div>
</div>Run Code Online (Sandbox Code Playgroud)
您还可以根据需要添加任意数量的掩码:
.green {
background: url(https://picsum.photos/id/1018/800/800) center/cover;
height: 100vh;
}
.blue {
background:#1b4287;
height: 100%;
-webkit-mask:
/* 3rd mask */
radial-gradient(farthest-side,#fff 99%,transparent)
top 50px right 50px/
100px 100px,
/**/
/* 2nd mask */
linear-gradient(#fff,#fff)
bottom 50px right 50px/
300px 20px,
/**/
/* 1st mask */
linear-gradient(#fff,#fff)
50px 50px/
200px 20px,
/**/
linear-gradient(#fff,#fff);
-webkit-mask-repeat:no-repeat;
-webkit-mask-composite: destination-out;
mask:
/* 3rd mask */
radial-gradient(farthest-side,#fff 99%,transparent)
top 50px right 50px/
100px 100px,
/**/
/* 2nd mask */
linear-gradient(#fff,#fff)
bottom 50px right 50px/
300px 20px,
/**/
/* 1st mask */
linear-gradient(#fff,#fff)
50px 50px/
200px 20px,
/**/
linear-gradient(#fff,#fff);
mask-repeat:no-repeat;
mask-composite:exclude;
transition:.5s;
}
.blue:hover {
-webkit-mask-position:
100px 100px,
bottom 100px left 50px,
top 50px right 50px,
0 0;
mask-position:
100px 100px,
bottom 100px left 50px,
top 50px right 50px,
0 0;
-webkit-mask-size:
150px 150px,
50px 50px,
300px 50px,
auto;
mask-size:
150px 150px,
50px 50px,
300px 50px,
auto;
}
body {
margin: 0;
}Run Code Online (Sandbox Code Playgroud)
<div class="green">
<div class="blue">
</div>
</div>Run Code Online (Sandbox Code Playgroud)