CSS3:鼠标悬停时动画变化之间的平滑过渡

16

我有一个元素,它有无限的 css3 动画,在鼠标悬停时会切换到另一个无限动画。一切都好,但有时动画切换过于弹跳,是否有办法在鼠标移入和移出时使动画之间的过渡更加平滑(也许是将元素带回动画之间的初始状态)?两个动画的起始和结束状态相同。

@keyframes first-animation {
0% { transform: scale3d(1,1,0);}
50% { transform: scale3d(0.8,0.8,0); }
100% { transform: scale3d(1,1,0); }
};

@keyframes second-animation {
0% { transform: scale3d(1,1,0); }
70% { transform: scale3d(0.7,0.7,0); }
80% { transform: scale3d(0.9,0.9,0); }
100% { transform: scale3d(1,1,0);  }
};

div{
animation: first-animation 1.7s ease-in-out infinite;
}

div:hover, div:focus{
animation: second-animation 1.1s ease-in-out infinite;
}

2
你是指纯CSS解决方案吗?如果是,那么答案是否定的。如果不是,你可以等待onAnimationEnd事件来切换它。 - Zach Saucier
为了获得更精确的动画效果,我建议不要使用 'ease-in-out',而是尝试使用 cubic-bezier()。可以在 http://cubic-bezier.com/ 上进行调试。 - ghaschel
3个回答

7

我认为无法用比例变换来实现。

你可以采用一种技巧,将 scale() 更改为 translateZ()。当应用透视时,净效果也将是缩放。但有趣的是,设置透视值较高时,这种比例效果可以变得非常小。透视是可动画的属性。

缺点是我们需要添加 2 个包装层... 但最终结果是这样的。我保留了原始版本进行比较。

@keyframes first-animation {
0% { transform: scale(1,1);}
50% { transform: scale(0.8,0.8); }
100% { transform: scale(1,1); }
}

@keyframes second-animation {
0% { transform: scale(1,1); }
70% { transform: scale(0.7,0.7); }
80% { transform: scale(0.9,0.9); }
100% { transform: scale(1,1);  }
}

.sample {
    background-color: lightblue;
    animation: first-animation 1.7s ease-in-out infinite;
}

.sample:hover {
animation: second-animation 1.1s ease-in-out infinite;
}

.dim {
    width: 200px;
    height: 200px;
}

.base1 {
    perspective: 1000px;
    transition: perspective 2s ease-out;
    position: absolute;
    left: 300px;
  top: 10px;
}
.base1:hover {
    perspective: 9999px;
}

.base2 {
    width: 100%;
    height: 100%;
    animation: animation1 1.7s ease-in-out infinite;
    perspective: 9999px;
    transition: perspective 2s ease-in;
}
.base1:hover .base2 {
    perspective: 1000px;
}

.inner {
    width: 100%;
    height: 100%;
    background-color: lightgreen;
    animation: animation2 1.1s ease-in-out infinite;
    transform-style: preserve-3d;
    perspective: 99999px;
}

@keyframes animation1 {
0% { transform: translateZ(0px);}
50% { transform: translateZ(-200px); }
100% { transform: translateZ(0px); }
}

@keyframes animation2 {
      0% { transform: translateZ(0px);}
     70% { transform: translateZ(-300px); }
     80% { transform: translateZ(-100px); }
    100% { transform: translateZ(0px); }
}
<div class="sample dim">SAMPLE</div>
<div class="base1 dim">
    <div class="base2">
        <div class="inner">DEMO</div>
    </div>
</div>


2
为了达到预期的效果,您需要使用css3动画事件。在这种情况下,您需要使用“AnimationIteration”。因此,您可以在迭代后触发事件。我已经为第二个动画添加了一个类。 Codepen演示
$(document).ready(function() {
  var animationElement = $(".animation");

  $("body").on({
    mouseover: function() {
      animationElement.one('webkitAnimationIteration mozAnimationIteration AnimationIteration', function() {
        animationElement.addClass("second-animation");
      });
    },
    mouseleave: function() {
      animationElement.one('webkitAnimationIteration mozAnimationIteration AnimationIteration', function() {
        animationElement.removeClass("second-animation");
      });
    }
  });
});

-1
你有用过transition吗?如果没有,请在父级div中添加过渡规则。
div{
-webkit-transition: all 500ms linear;
-moz-transition: all 500ms linear;
-ms-transition: all 500ms linear;
-o-transition: all 500ms linear;
transition: all 500ms linear;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接