简体   繁体   English

如何使用关键帧在鼠标移开时实现反向动画

[英]How to achieve reverse animation on mouse out using keyframes

I am trying to add scale up animation on a div.我正在尝试在 div 上添加放大动画。 I tried this using both transition and animation property.我尝试使用transitionanimation属性。

In case of transition you can notice that when hovered out the animation is smoothly reversed.transition情况下,您可以注意到当悬停时动画平滑反转。 However, this doesn't happen when using animation property (the div transitions back to initial width instantly)但是,使用animation属性时不会发生这种情况(div 立即转换回初始宽度)

Can someone tell me:谁能告诉我:

  1. Why this behaviour in case of animation only?为什么仅在animation情况下会出现这种行为?
  2. How can I achieve the same using animation property?如何使用animation属性实现相同的效果?

 .animations { display: flex; padding: 80px; width: 100vw; height: 100vh; background: linear-gradient(to right, #f3d2d2, white, #cee5f3); } .animations > div { width: 200px; height: 200px; margin: 40px; font-family: system-ui; display: flex; flex-direction: column; align-items: center; } .animations > p { color: black; flex: 1; text-align: center; } .animations .animated-box { flex: 2; width: 100%; background: grey; cursor: pointer; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: white; } .animated-box.scale-up { } .animated-box.scale-up:hover { animation: scale-up 0.5s ease forwards; transform: scale(1); } .animated-box.scale-up-with-mouseout { transition: transform 0.5s ease-in; } .animated-box.scale-up-with-mouseout:hover { transform: scale(1.2); } @keyframes scale-up { 100% {transform: scale(1.2)}; 0%{transform: scale(1)}; }
 <div class="animations"> <div> <div class="animated-box scale-up">Hover me</div> <p>Scale up (with keyframes)</p> </div> <div> <div class="animated-box scale-up-with-mouseout">Hover me</div> <p>Scale up (with transition)</p> </div> </div>

invert this part only只反转这部分

@keyframes scale-up {
  100% {transform: scale(1.2)};
  0%{transform: scale(1)};
}

and to fix the animation when mouse out add a new keyframe并在鼠标移开时修复动画添加新的keyframe

@keyframes scale-down {
  0% {transform: scale(1.2)};
  100%{transform: scale(1)};
}

and apply it to the class .animated-box.scale-up并将其应用于类.animated-box.scale-up

.animated-box.scale-up {
   animation: scale-down 0.5s ease forwards;
}

 .animations { display: flex; padding: 80px; width: 100vw; height: 100vh; background: linear-gradient(to right, #f3d2d2, white, #cee5f3); } .animations > div { width: 200px; height: 200px; margin: 40px; font-family: system-ui; display: flex; flex-direction: column; align-items: center; } .animations > p { color: black; flex: 1; text-align: center; } .animations .animated-box { flex: 2; width: 100%; background: grey; cursor: pointer; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: white; } .animated-box.scale-up { animation: scale-down 0.5s ease forwards; } .animated-box.scale-up:hover { animation: scale-up 0.5s ease forwards; } .animated-box.scale-up-with-mouseout { transition: transform 0.5s ease-in; } .animated-box.scale-up-with-mouseout:hover { transform: scale(1.2); } @keyframes scale-up { 100% {transform: scale(1.2)}; 0%{transform: scale(1)}; } @keyframes scale-down { 0% {transform: scale(1.2)}; 100%{transform: scale(1)}; }
 <div class="animations"> <div> <div class="animated-box scale-up">Hover me</div> <p>Scale up (doesn't work)</p> </div> <div> <div class="animated-box scale-up-with-mouseout">Hover me</div> <p>Scale up (works)</p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM