【问题标题】:Masking a circle and moving the mask with animation遮罩一个圆圈并用动画移动遮罩
【发布时间】:2017-06-28 16:35:21
【问题描述】:

我有一个在 div 中垂直和水平居中的圆形 div。我正在尝试实现一个css动画,它似乎从上到下淡入淡出。

我最初想将高度设为 0 并移动到 50px,但是当它居中时,它开始从中心创建。相反,我想让它定位到初始位置,并从上到下创建它。就像有一个正方形只遮住了圆形而没有其他东西一样,它会向下移动。

#circle {
   width: 80px;
   height: 80px;
   border-radius: 50px;
}

如何添加这个方形蒙版来达到下面的效果?

请注意,背景有渐变,所以我不能直接放置一个正方形并为其分配颜色,因此我认为我需要遮盖它们。

如何实现这个效果?


我尝试过的:

@keyframes example {
   from {height: 0}
   to {height: 80px}
}

当圆居中时,它从中间开始扩大。这不是我想要的。这就是为什么我想到了面具

【问题讨论】:

  • 向我们展示您迄今为止所做的尝试。
  • 您可能必须使用图像背景或为对象生成渐变背景。请参阅下面的答案。
  • “Nants ingonyama bagithi baba Sithi uhm ingonyama Nants ingonyama bagithi baba Sithi uhm ingonyama Ingonyama” - 狮子王介绍

标签: html css css-animations mask


【解决方案1】:

修改后的答案:

我可以通过图像backgroundbackground-position 动画的组合来实现这一点。

如果您将背景设置为 #fff 之类的 CSS 颜色,这将不起作用。它需要是图像或渐变。您还需要将background-repeat 设置为no-repeat

动画只是从 div 显示区域的背景 out 开始,然后将背景向下拉到显示区域中。

请全屏查看示例。

工作 sn-p(jpeg 图像作为对象背景):

body {
  background: linear-gradient(135deg, rgba(244, 226, 156, 0) 0%, rgba(59, 41, 58, 1) 100%), linear-gradient(to right, rgba(244, 226, 156, 1) 0%, rgba(130, 96, 87, 1) 100%);
  margin: 0 auto;
  height: 120vh;
  overflow: hidden;
}

.sun {
  height: 400px;
  width: 400px;
  border-radius: 100vw;
  margin: 5em auto;
  animation-name: sunrise;
  animation-duration: 10s;
  animation-delay: .5s;
  animation-timing-function: linear;
  animation-iteration-count: 1;
  background: url(https://image.ibb.co/eVdQ3Q/white.jpg);
  background-repeat: no-repeat;
  animation-fill-mode: forwards;
  opacity: 0;
}

@keyframes sunrise {
  from {
  opacity: 1;
    background-position: 0 -700px;
  }
  to {
  opacity: 1;
    background-position: 0 0px;
  }
}
<div class="sun"></div>

工作 sn-p(渐变作为对象背景):

body {
  background: linear-gradient(135deg, rgba(244, 226, 156, 0) 0%, rgba(59, 41, 58, 1) 100%), linear-gradient(to right, rgba(244, 226, 156, 1) 0%, rgba(130, 96, 87, 1) 100%);
  margin: 0 auto;
  height: 120vh;
  overflow: hidden;
}

.sun {
  height: 400px;
  width: 400px;
  border-radius: 100vw;
  margin: 5em auto;
  animation-name: sunrise;
  animation-duration: 10s;
  animation-timing-function: linear;
  animation-iteration-count: 1;
  background: linear-gradient(white,white);
  background-repeat: no-repeat;
  animation-fill-mode: forwards;
  opacity: 0;
}

@keyframes sunrise {
  from {
  opacity: 1;
    background-position: 0 -700px;
  }
  to {
  opacity: 1;
    background-position: 0 0px;
  }
}
<div class="sun"></div>

【讨论】:

  • 这是一个非常简洁的例子!感谢分享!但是我需要类似的东西,圆圈是白色的,背景是渐变的。并使其从上到下创建。你似乎知道 css 动画;如果您能建议我一个方法,我将不胜感激!再次感谢
  • @senty 不客气。不,您不需要明确使用图像。您可以使用渐变作为对象的背景。我更新了答案以显示两个示例。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-07-20
  • 2018-04-15
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
相关资源
最近更新 更多