使用 SVG 裁剪/遮罩元素
Clipping / Masking element with SVG
我有以下内容:
一个 .circle
里面有一个 .object
。请参阅下面的代码(并查看全屏示例)。
我希望 .object
从底部开始(动画),所以圆可以是 overflow: hidden;
,但我希望 .object
从顶部出来。最终结果见 this image。
简而言之: .object
只应与其底部的 .circle
相同。
所以我认为 svg
掩码(底部边界半径为 50% 的正方形)可以解决问题。到目前为止,没有运气能让它工作。
我错过了什么吗?我没有正确使用它吗? SVG 不能用于屏蔽 div
个元素吗?还是有另一个 明显 解决这个问题的方法?
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.container {
background: gold;
width: 100%;
height: 100%;
position: relative;
}
.circle {
background: LightCoral;
width: 300px;
height: 300px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
clip-path: url(#clipping);
}
.object {
background: DeepSkyBlue;
width: 150px;
height: 350px;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
svg {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="container">
<div class="circle">
<div class="object"></div>
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 300 300">
<defs>
<clipPath id="clipping">
<path class="cls-1" d="M0,0H300V150H0V0ZM300,150A150,150,0,0,1,0,150" />
</clipPath>
</defs>
</svg>
</div>
</div>
您不需要为此使用 SVG 剪辑或蒙版。只需将您的对象包裹在具有圆底且溢出设置为隐藏的 div 中。
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.container {
background: gold;
width: 100%;
height: 100%;
position: relative;
}
.circle {
background: LightCoral;
width: 300px;
height: 300px;
position: absolute;
left: 50%;
transform: translate(-50%, 0);
border-radius: 50%;
}
.mask {
width: 300px;
height: 1000px;
position: absolute;
left: 50%;
bottom: 0;
transform: translateX(-50%);
border-bottom-left-radius: 150px;
border-bottom-right-radius: 150px;
overflow: hidden;
}
.object {
background: DeepSkyBlue;
width: 150px;
height: 350px;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
<div class="container">
<div class="circle">
<div class="mask">
<div class="object"></div>
</div>
</div>
</div>
我有以下内容:
一个 .circle
里面有一个 .object
。请参阅下面的代码(并查看全屏示例)。
我希望 .object
从底部开始(动画),所以圆可以是 overflow: hidden;
,但我希望 .object
从顶部出来。最终结果见 this image。
简而言之: .object
只应与其底部的 .circle
相同。
所以我认为 svg
掩码(底部边界半径为 50% 的正方形)可以解决问题。到目前为止,没有运气能让它工作。
我错过了什么吗?我没有正确使用它吗? SVG 不能用于屏蔽 div
个元素吗?还是有另一个 明显 解决这个问题的方法?
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.container {
background: gold;
width: 100%;
height: 100%;
position: relative;
}
.circle {
background: LightCoral;
width: 300px;
height: 300px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
clip-path: url(#clipping);
}
.object {
background: DeepSkyBlue;
width: 150px;
height: 350px;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
svg {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="container">
<div class="circle">
<div class="object"></div>
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 300 300">
<defs>
<clipPath id="clipping">
<path class="cls-1" d="M0,0H300V150H0V0ZM300,150A150,150,0,0,1,0,150" />
</clipPath>
</defs>
</svg>
</div>
</div>
您不需要为此使用 SVG 剪辑或蒙版。只需将您的对象包裹在具有圆底且溢出设置为隐藏的 div 中。
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.container {
background: gold;
width: 100%;
height: 100%;
position: relative;
}
.circle {
background: LightCoral;
width: 300px;
height: 300px;
position: absolute;
left: 50%;
transform: translate(-50%, 0);
border-radius: 50%;
}
.mask {
width: 300px;
height: 1000px;
position: absolute;
left: 50%;
bottom: 0;
transform: translateX(-50%);
border-bottom-left-radius: 150px;
border-bottom-right-radius: 150px;
overflow: hidden;
}
.object {
background: DeepSkyBlue;
width: 150px;
height: 350px;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
<div class="container">
<div class="circle">
<div class="mask">
<div class="object"></div>
</div>
</div>
</div>