CSS Animation with different image based on the background

Viewed 82

I am using CSS animation to show different images based on the background color but the image position should not be changed and the background color animation should happen from bottom to top has the background color fill I should able to see the respective image for that particular background color on hover.

HTML

div class="card">
  <div class="card-img">
    <div>
        <img src="../../images/assets/kidney_empty.svg" alt="">
    </div>
    <div>
        <img src="../../images/assets/kidneys_rev.svg" alt="">
    </div>
  </div>
  <div class="card-body">
    <div class="content-selector-wrapper">
      <paragraph>
        <p><strong>Renal manifestations</strong> can progress to kidney failure.</p>
      </paragraph>
    </div>
  </div>
</div>

CSS

.mult-image-animation {
  text-align             : center;
  }
  .mult-image-animation .card-img {
    width                : 150px;
    height               : 150px;
    border-radius        : 50%;
    overflow             : hidden;
    margin-bottom        : 35px;
    position             : relative;
    margin-left          : auto;
    margin-right         : auto;
    }
    .mult-image-animation .card-img > div {
      position           : absolute;
      width              : 150px;
      height             : 150px;
      margin-left        : auto;
      margin-right       : auto;
      padding            : 40px 30px;
      max-width          : 100%;
      top                : 0;
      bottom             : 0;
      right              : 0;
      left               : 0;
      margin             : auto;
      height             : 150px;
      -webkit-transition : all ease 3s;
      -moz-transition    : all ease 3s;
      transition         : all ease 3s;
      }
    .mult-image-animation .card-img div:first-child {
      z-index            : 1;
      }
  .mult-image-animation .card-body {
    padding              : 0;
    font-size            : 18px;
    color                : #4D4D4F;
    font-family          : "Merriweather", Helvetica, Arial, sans-serif;
    }
    .mult-image-animation .card-body strong {
      font-family        : "Merriweather-Bold", Helvetica, Arial, sans-serif;
      }
  .mult-image-animation.trigger-animation .card-img div:last-child {
    box-shadow           : inset 0 -70px 0 0 #EA1010;
    z-index              : 5;
    }

I am able to get the animation and the background image but has I hover it's hiding the previous image but I want the previous image to hide as the transition completes. Where i should be able to see both outline and solid image like this

enter image description here

JS Fiddle Image https://jsfiddle.net/prasadau1989/m9abLo4f/ Is there a way for me to achieve this in SVG? https://jsfiddle.net/prasadau1989/m9abLo4f/1/

Any guide is highly appreciated.

1 Answers

You're actually pretty close, and SVG was a great idea! Using SVG makes it easy to swap the fill color on an element however you want depending on the context.

Essentially, you want to create the same element twice with inverted colors, but don't mess with z-index at all. Instead, create a clip-path for the hover-state, already set to be fully clipped from the top:

.mult-image-animation .card-img > div:last-child {
    clip-path: inset(100% 0% 0% 0%);
}

Then on hover, set that 100% value back to 0% to create the animation where it appears the animation starts at the bottom as you described.

Here it is, all put together:

:root {
    --static-color: #fff;
    --accent-color: #ea1010;
}

.mult-image-animation {
    text-align: center;
}

.mult-image-animation .card-img {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    overflow: hidden;
    margin-bottom: 35px;
    position: relative;
    margin-left: auto;
    margin-right: auto;
    box-shadow: inset 0 0 0 4px var(--accent-color);
}

.mult-image-animation .card-img > div {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    padding: 15px;
    transition: all 0.5s ease-out;
    box-sizing: border-box;
    overflow: hidden;
    z-index: -1;
}

.mult-image-animation .card-img > div:first-child {
    background-color: var(--static-color);
}

.mult-image-animation .card-img > div:first-child > svg {
    fill: var(--accent-color);
}

.mult-image-animation .card-img > div:last-child {
    background-color: var(--accent-color);
    -webkit-clip-path: inset(100% 0% 0% 0%);
            clip-path: inset(100% 0% 0% 0%);
}

.mult-image-animation .card-img > div:last-child > svg {
    fill: var(--static-color);
}

.mult-image-animation .card-body {
    padding: 0;
    font-size: 18px;
    color: #4D4D4F;
    font-family: "Merriweather", Helvetica, Arial, sans-serif;
}

.mult-image-animation .card-body strong {
    font-family: "Merriweather-Bold", Helvetica, Arial, sans-serif;
}

.mult-image-animation:hover .card-img div:last-child {
    -webkit-clip-path: inset(0% 0% 0% 0%);
            clip-path: inset(0% 0% 0% 0%);
}

img {
    max-width: 100%;
}
<div class="mult-image-animation">
    <div class="card">
        <div class="card-img">
            <div>
                <svg width="100px" height="100px" version="1.1" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
                <g>
                    <path d="m50 11.668c-1.25 0-2.082 0.83203-2.082 2.082v72.293c0 1.25 0.83203 2.082 2.082 2.082s2.082-0.83203 2.082-2.082v-72.293c0-1.043-0.83203-2.082-2.082-2.082z" />
                    <path d="m100 33.75c-0.20703-10.418-3.332-18.125-9.375-22.707-9.375-7.5-22.293-4.793-22.5-4.5859-3.957 0.83203-11.043 5.418-13.75 12.5-1.457 3.75-1.875 9.375 2.918 16.043 2.293 3.125 8.332 4.793 11.875 8.543l-1.25 0.20703c-5.418 1.043-9.582 5.832-9.582 11.457v37.082c0 1.25 0.83203 2.082 2.082 2.082s2.082-0.83203 2.082-2.082v-37.289c0-3.543 2.5-6.668 6.043-7.293l2.5-0.41797c0.625 2.918 0 5.832-1.668 8.125-6.25 8.332-0.83203 20.832 13.332 20.832 3.957 0 8.75-1.668 12.5-9.168 3.9609-7.0781 5-21.871 4.793-33.328zm-8.332 31.457c-4.582 8.543-9.375 7.082-13.957 6.043-5.832-1.457-8.543-8.332-4.793-13.125 2.5-3.332 3.332-7.5 2.5-11.668-0.20703-0.83203-0.41797-1.668-0.83203-2.293-3.125-6.668-11.875-8.957-13.75-11.457-2.918-4.168-3.75-8.125-2.293-12.082 1.875-6.043 7.9141-9.582 10.625-10.207 4.793-1.043 13.332-0.625 18.957 3.75 5 3.957 7.707 10.418 7.707 19.582 0.21094 10.625-0.83203 25-4.1641 31.457z" />
                    <path d="m31.875 6.457c-0.20703 0-13.125-2.9141-22.5 4.5859-6.043 4.793-9.168 12.5-9.375 22.707-0.20703 11.457 0.83203 26.25 4.582 33.332 3.957 7.293 8.543 9.168 12.5 9.168 13.957 0 19.582-12.5 13.332-20.832-1.668-2.293-2.293-5.207-1.668-8.125 2.082 0.41797 8.543 1.043 8.543 7.707v37.082c0 1.25 0.83203 2.082 2.082 2.082s2.082-0.83203 2.082-2.082l0.003906-37.082c0-5.207-3.332-10.207-10.625-11.668 3.543-3.75 9.582-5.418 11.875-8.543 4.793-6.668 4.168-12.293 2.918-16.043-2.707-6.8711-9.793-11.453-13.75-12.289zm7.5 25.836c-2.082 2.707-12.5 5.418-14.582 13.75-1.043 4.168 0 8.543 2.5 11.668 3.543 4.582 1.457 11.457-5.207 13.332-4.375 1.043-9.168 2.293-13.543-6.043-3.332-6.25-4.375-20.832-4.168-31.25 0.20703-9.168 2.707-15.625 7.707-19.582 5.832-4.582 14.168-4.793 18.957-3.75 2.5039 0.625 8.543 4.1641 10.629 9.7891 1.457 3.9609 0.625 8.125-2.293 12.086z" />
                </g>
            </svg>
            </div>
            <div>
                <svg width="100px" height="100px" version="1.1" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
                <g>
                    <path d="m50 11.668c-1.25 0-2.082 0.83203-2.082 2.082v72.293c0 1.25 0.83203 2.082 2.082 2.082s2.082-0.83203 2.082-2.082v-72.293c0-1.043-0.83203-2.082-2.082-2.082z" />
                    <path d="m100 33.75c-0.20703-10.418-3.332-18.125-9.375-22.707-9.375-7.5-22.293-4.793-22.5-4.5859-3.957 0.83203-11.043 5.418-13.75 12.5-1.457 3.75-1.875 9.375 2.918 16.043 2.293 3.125 8.332 4.793 11.875 8.543l-1.25 0.20703c-5.418 1.043-9.582 5.832-9.582 11.457v37.082c0 1.25 0.83203 2.082 2.082 2.082s2.082-0.83203 2.082-2.082v-37.289c0-3.543 2.5-6.668 6.043-7.293l2.5-0.41797c0.625 2.918 0 5.832-1.668 8.125-6.25 8.332-0.83203 20.832 13.332 20.832 3.957 0 8.75-1.668 12.5-9.168 3.9609-7.0781 5-21.871 4.793-33.328zm-8.332 31.457c-4.582 8.543-9.375 7.082-13.957 6.043-5.832-1.457-8.543-8.332-4.793-13.125 2.5-3.332 3.332-7.5 2.5-11.668-0.20703-0.83203-0.41797-1.668-0.83203-2.293-3.125-6.668-11.875-8.957-13.75-11.457-2.918-4.168-3.75-8.125-2.293-12.082 1.875-6.043 7.9141-9.582 10.625-10.207 4.793-1.043 13.332-0.625 18.957 3.75 5 3.957 7.707 10.418 7.707 19.582 0.21094 10.625-0.83203 25-4.1641 31.457z" />
                    <path d="m31.875 6.457c-0.20703 0-13.125-2.9141-22.5 4.5859-6.043 4.793-9.168 12.5-9.375 22.707-0.20703 11.457 0.83203 26.25 4.582 33.332 3.957 7.293 8.543 9.168 12.5 9.168 13.957 0 19.582-12.5 13.332-20.832-1.668-2.293-2.293-5.207-1.668-8.125 2.082 0.41797 8.543 1.043 8.543 7.707v37.082c0 1.25 0.83203 2.082 2.082 2.082s2.082-0.83203 2.082-2.082l0.003906-37.082c0-5.207-3.332-10.207-10.625-11.668 3.543-3.75 9.582-5.418 11.875-8.543 4.793-6.668 4.168-12.293 2.918-16.043-2.707-6.8711-9.793-11.453-13.75-12.289zm7.5 25.836c-2.082 2.707-12.5 5.418-14.582 13.75-1.043 4.168 0 8.543 2.5 11.668 3.543 4.582 1.457 11.457-5.207 13.332-4.375 1.043-9.168 2.293-13.543-6.043-3.332-6.25-4.375-20.832-4.168-31.25 0.20703-9.168 2.707-15.625 7.707-19.582 5.832-4.582 14.168-4.793 18.957-3.75 2.5039 0.625 8.543 4.1641 10.629 9.7891 1.457 3.9609 0.625 8.125-2.293 12.086z" />
                </g>
            </svg>
            </div>
        </div>
        <div class="card-body">
            <div class="content-selector-wrapper">
                <paragraph>
                    <p><strong>Renal manifestations</strong> can progress to kidney failure.</p>
                </paragraph>
            </div>
        </div>
    </div>
</div>

I hope this helps! Feel free to ask me any follow-up questions in the comments. Cheers!

Related