Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to animate colour of an image from left to right?

I'm trying to animate a loading icon using an image. What I'm trying to achieve is to have the image animate between two colours from left to right infinitely. This is done in Angular 7. I'm new to animations but as far as I understand I can solve this with CSS animations.

What I want is something similar to the example below. Only I want the grayscale to disappear to the right, and instead of a grayscale I want to pick two different colours.

All help is appreciated!

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>
like image 265
kebbaben Avatar asked Dec 31 '25 03:12

kebbaben


1 Answers

You can consider an extra layer and mix-blend-mode. Simply adjust the coloration and the blending to obtain the color you want. You will no more need to duplicate the image.

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>
like image 79
Temani Afif Avatar answered Jan 06 '26 08:01

Temani Afif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!