【问题标题】:Clipping / Masking element with SVG使用 SVG 剪辑/遮罩元素
【发布时间】:2016-03-27 17:11:01
【问题描述】:

我有以下:

一个.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 中使用蒙版,稍后将发布代码。
  • @MichaelMullany 我添加了我想要实现的最终结果的图像,这是否足够清楚?
  • 你的圆圈和蓝色对象需要是 HTML 元素吗?如果所有元素都是 SVG 元素,这将非常容易。

标签: css svg mask


【解决方案1】:

您不需要为此使用 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>

【讨论】:

  • 嗯,这是我想不出的显而易见的答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 2019-04-06
  • 2012-09-23
  • 1970-01-01
相关资源
最近更新 更多