【问题标题】:Is there a way to animate colour of an image from left to right?有没有办法从左到右为图像的颜色设置动画?
【发布时间】:2019-10-25 21:40:03
【问题描述】:

我正在尝试使用图像为加载图标设置动画。我想要实现的是让图像从左到右无限地在两种颜色之间动画。这是在 Angular 7 中完成的。我是动画新手,但据我所知,我可以用 CSS 动画解决这个问题。

我想要的是类似于下面的示例。只有我希望灰度消失到右侧,而不是灰度我想选择两种不同的颜色。

感谢所有帮助!

body {
  margin: 0;
}

.effect {
  position: relative;
}

.effect-dup {
  filter: grayscale(100%);
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  width: 0;
  animation: width 1s ease-in-out;
  animation-fill-mode: forwards;
}

@keyframes width {
  from {
    width: 0;
  }
  to {
    width: 100%;
  }
}
<div class="effect">
  <img src="https://placeimg.com/640/480/nature" />
  <div class='effect-dup'><img src="https://placeimg.com/640/480/nature" /></div>
</div>

<div class="effect">
  <img src="https://placeimg.com/640/480/tech" />
  <div class='effect-dup'>
    <img src="https://placeimg.com/640/480/tech" />
  </div>
</div>

【问题讨论】:

  • filter: grayscale(100%)改成你想要的filter...
  • 您可以使用 CSS 动画,也可以使用基于 Angular 的动画:angular.io/guide/animations。有选择总是很高兴:)
  • @HereticMonkey 感谢您的快速回复!因此,假设我已经设法获得了我想要的过滤器。然后如何在动画中的两个过滤器之间进行切换?
  • 它已经动画化了无过滤器和过滤器之间的变化。所以给初始状态添加一个过滤器(.effect)。

标签: css angular sass css-animations


【解决方案1】:

您可以考虑增加一层和mix-blend-mode。只需调整着色和混合即可获得您想要的颜色。您将不再需要复制图像。

body {
  margin: 0;
}

img {
  width: 200px;
  display: block;
}

.effect {
  position: relative;
  display: inline-block;
  overflow:hidden;
}

.effect:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  animation: width 2s ease-in-out infinite;
  mix-blend-mode: color;
  background: linear-gradient(blue,red);
}

@keyframes width {
  from {
    transform:translateX(-100%);
  }
  to {
    transform:translateX(100%);
  }
}
<div class="effect">
  <img src="https://placeimg.com/640/480/nature" />
</div>

<div class="effect">
  <img src="https://placeimg.com/640/480/tech" />
</div>

【讨论】:

  • 我不确定这是否是 OP 想要的,但它看起来确实很棒?
  • 除了@Temani 回答,要无限期运行,可以使用`animation-iteration-count:infinite;`css 属性
  • @TemaniAfif 我已经尝试过添加眼镜蛇的解决方案,但后来我得到了一个跳跃动画。我希望过滤器消失到一侧并出现在另一侧。这可行吗?
  • @kebbaben 将alternate 添加到动画中:animation: width 5s ease-in-out infinte alternate;
  • @TemaniAfif 那只会产生有弹性的效果。我希望动画出现在左侧并在右侧消失,然后再做一次,就像循环一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多