【问题标题】:CSS Animation with different image based on the background基于背景的不同图像的 CSS 动画
【发布时间】:2021-02-19 16:08:59
【问题描述】:

我正在使用 CSS 动画根据背景颜色显示不同的图像,但图像位置不应更改,并且背景颜色动画应该从下到上发生具有背景颜色填充我应该能够看到相应的图像悬停时的特定背景颜色。

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;
    }

我能够获得动画和背景图像,但我悬停它会隐藏上一张图像,但我希望在过渡完成时隐藏上一张图像。在哪里我应该能够看到这样的轮廓和实体图像

JS 小提琴图片 https://jsfiddle.net/prasadau1989/m9abLo4f/ 有没有办法让我在 SVG 中实现这一点? https://jsfiddle.net/prasadau1989/m9abLo4f/1/

非常感谢任何指南。

【问题讨论】:

    标签: html jquery css animation svg


    【解决方案1】:

    您实际上已经很接近了,SVG 是个好主意!使用 SVG 可以轻松地根据上下文交换元素上的填充颜色。

    本质上,您想用倒色创建两次相同的元素,但不要乱用z-index。相反,为悬停状态创建一个clip-path,已经设置为从顶部完全剪裁:

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

    然后在悬停时,将 100% 值设置回 0% 以创建动画,动画显示在您描述的底部开始。

    全部放在一起:

    :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>

    我希望这会有所帮助!请随时在 cmets 中向我提出任何后续问题。干杯!

    【讨论】:

    • 有没有办法在 IE 或 firefox 中支持这一点?因为这个解决方案只适用于 chrome
    • 是的,我使用 autoprefixer,所以我忘了包含 webkit 的供应商前缀。请相应地添加-webkit-clip-path: inset(100% 0% 0% 0%);-webkit-clip-path: inset(0% 0% 0% 0%);(同时包括clip-path-webkit-clip-path。这应该适用于所有现代浏览器(包括Edge,但不是IE)。你可以在Firefox中测试它(运行自动前缀) :codepen.io/brandonmcconnell/pen/…
    • 我在上面的解决方案中调整了 CSS 以反映 -webkit-clip-path 供应商前缀。我希望这有帮助!如果这对您有用,请接受我对这个问题的回答 :) 否则,我仍然可以帮助解决我提供的解决方案中的任何其他问题。
    猜你喜欢
    • 2021-06-03
    • 2013-06-26
    • 2023-03-14
    • 2015-04-22
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2010-10-23
    相关资源
    最近更新 更多