【问题标题】:Reverse css keyframe animation反转css关键帧动画
【发布时间】:2019-09-28 05:30:27
【问题描述】:

我已经使用关键帧为 svg 路径编写了一个 css 动画

path {
  animation: anim 1s linear;
  animation-fill-mode: forwards;
}

@keyframes anim {
  50% {
    d: path('M20 20 L 20 20');
  }
  100% {
    d: path('M10 10 L 30 30');
  }
}
<path class="top" d="M10 10 L 30 10" stroke-width="2" stroke="black" fill="transparent" />

Source

如果您单击 svg,您可以看到正在运行的动画。现在,如果您再次单击,我希望它动画到其原始状态。我试过添加

transition: all 1s ease-out;

animation-direction: alternate;

但是仍然没有动画回到它的初始状态。

所以问题是,我怎样才能动画回来?

【问题讨论】:

    标签: css svg css-animations keyframe


    【解决方案1】:

    使用过渡,您可以轻松做到这一点,但动画会略有不同:

    const svg = document.querySelector('svg');
    
    svg.addEventListener('click', () => {
      svg.classList.toggle('close');
    });
    svg {
      border: 1px solid blue;
    }
    
    svg path {
      transition: 0.2s all linear;
    }
    
    svg.close path.top {
      d: path('M10 10 L 30 30');
    }
    
    svg.close path.middle {
      d: path('M20 20 L 20 20');
    }
    
    svg.close path.bottom {
      d: path('M10 30 L 30 10');
    }
    <svg width="190" height="160" xmlns="http://www.w3.org/2000/svg">
    
      <path class="top" d="M10 10 L 30 10" stroke-width="2" stroke="black" fill="transparent"/>
      <path class="middle" d="M10 20 L 30 20" stroke-width="2" stroke="black" fill="transparent"/>
      <path class="bottom" d="M10 30 L 30 30" stroke-width="2" stroke="black" fill="transparent"/>
    </svg>

    另一个想法是依赖animationiteration,其中诀窍是运行infinite 动画并在每次迭代时停止它。

    const svg = document.querySelector('svg');
    
    const path = document.querySelector('svg path');
    
    svg.addEventListener('click', () => {
      svg.classList.toggle('close');
    });
    
    
    path.addEventListener('animationiteration', () =>{
     svg.classList.toggle('close');
    });
    svg {
      border: 1px solid blue;
    }
    
    
    path.top {
      animation: close-top 0.2s linear infinite alternate;
    }
    
    path.middle {
      animation: close-middle 0.2s linear infinite alternate;
    }
    
    path.bottom {
      animation: close-bottom 0.2s linear infinite alternate;
    }
    
    svg:not(.close) path {
     animation-play-state: paused;
    }
    
    svg.close path {
     animation-play-state: running;
    }
    
    @keyframes close-top {
      50% {
        d: path('M20 20 L 20 20');
      }
      100% {
        d: path('M10 10 L 30 30');
      }
    }
    
    @keyframes close-middle {
      to {
        d: path('M20 20 L 20 20');
      }
    }
    
    @keyframes close-bottom {
      50% {
        d: path('M20 20 L 20 20');
      }
      100% {
        d: path('M10 30 L 30 10');
      }
    }
    <svg width="190" height="160" xmlns="http://www.w3.org/2000/svg">
    
      <path class="top" d="M10 10 L 30 10" stroke-width="2" stroke="black" fill="transparent"/>
      <path class="middle" d="M10 20 L 30 20" stroke-width="2" stroke="black" fill="transparent"/>
      <path class="bottom" d="M10 30 L 30 30" stroke-width="2" stroke="black" fill="transparent"/>
    </svg>

    【讨论】:

    • 善用animation-play-state
    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多