【问题标题】:How to reverse animation on mouse out after hover悬停后如何在鼠标移出时反转动画
【发布时间】:2015-11-27 03:00:21
【问题描述】:

这正是我想要实现的(动画在我悬停时开始,在我悬停后恢复)。我只是不希望动画开始,直到我将鼠标悬停在对象上。在代码中,动画在刷新后立即开始。

.class {
  animation-name: out;
  animation-duration: 2s;
  /* Safari and Chrome: */
  -webkit-animation-name: out;
  -webkit-animation-duration: 2s;
}
.class:hover {
  animation-name: in;
  animation-duration: 5s;
  animation-iteration-count: infinite;
  animation-direction: normal;
  /* Safari and Chrome: */
  -webkit-animation-name: in;
  -webkit-animation-duration: 5s;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-direction: alternate;
}
@keyframes in {
  from {
    transform: rotate(50deg);
  }
  to {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes in
/* Safari and Chrome */

{
  from {
    transform: rotate(50deg);
  }
  to {
    -webkit-transform: rotate(360deg);
  }
}
@keyframes out {
  from {
    transform: rotate(360deg);
  }
  to {
    transform: rotate(0deg);
  }
}
@-webkit-keyframes out
/* Safari and Chrome */

{
  from {
    transform: rotate(360deg);
  }
  to {
    -webkit-transform: rotate(0deg);
  }
}
<div style="width:100px; height:100px; background-color:red" class="class"></div>

【问题讨论】:

    标签: javascript css css-animations


    【解决方案1】:

    你可以去掉动画,直接在类上添加transformtransition属性,如下所示:

    .class {
      transform: rotate(0deg);
      transition: transform 2s;
    }
    
    .class:hover {
      transform: rotate(360deg);
      transition: transform 5s;
    }
    <div style="width:100px; height:100px; background-color:red" class="class"></div>

    【讨论】:

      猜你喜欢
      • 2017-08-07
      • 2016-10-30
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 2011-05-26
      • 2021-11-13
      相关资源
      最近更新 更多