html - css animation+transition 每 10 秒触发一次并且在悬停时触发

标签 html css

我正在使用这段代码每 10 秒触发一次 css 动画

@keyframes tada_9122 {
      0% { transform: scale(1) }
      0.63291% { transform:scale(.9) rotate(-2deg) }
      1.26582% { transform:scale(.9) rotate(-1deg) }
      1.89873% { transform:scale(1.1) rotate(2deg) }
      2.53165% { transform:scale(1.1) rotate(-1deg) }
      3.16456% { transform:scale(1.1) rotate(2deg) }
      3.79747% { transform:scale(1.1) rotate(-1deg) }
      4.43038% { transform:scale(1.1) rotate(1deg) }
      5.06329% { transform:scale(1) rotate(0) }
      100% { transform:scale(1) rotate(0) }
    }
    
    .callToAction {
      animation: tada_9122 10s linear infinite;
      transform-origin: 50% 50%;
    }
<div class="callToAction" style="width:100px;height:100px;background-color:red;">

</div>

但是我怎样才能在悬停 .callToAction 时触发相同的动画呢?

这行不通:

.callToAction:hover {
  animation: tada_9122 10s;
  transform-origin: 50% 50%;
}

最佳答案

解决方案是用另一个名称创建另一个关键帧并在悬停时使用

@keyframes tada_9122 {
  0% {
    transform: scale(1)
  }
  0.63291% {
    transform: scale(.9) rotate(-2deg)
  }
  1.26582% {
    transform: scale(.9) rotate(-1deg)
  }
  1.89873% {
    transform: scale(1.1) rotate(2deg)
  }
  2.53165% {
    transform: scale(1.1) rotate(-1deg)
  }
  3.16456% {
    transform: scale(1.1) rotate(2deg)
  }
  3.79747% {
    transform: scale(1.1) rotate(-1deg)
  }
  4.43038% {
    transform: scale(1.1) rotate(1deg)
  }
  5.06329% {
    transform: scale(1) rotate(0)
  }
  100% {
    transform: scale(1) rotate(0)
  }
}

@keyframes tada_9122_2 {
  0% {
    transform: scale(1)
  }
  0.63291% {
    transform: scale(.9) rotate(-2deg)
  }
  1.26582% {
    transform: scale(.9) rotate(-1deg)
  }
  1.89873% {
    transform: scale(1.1) rotate(2deg)
  }
  2.53165% {
    transform: scale(1.1) rotate(-1deg)
  }
  3.16456% {
    transform: scale(1.1) rotate(2deg)
  }
  3.79747% {
    transform: scale(1.1) rotate(-1deg)
  }
  4.43038% {
    transform: scale(1.1) rotate(1deg)
  }
  5.06329% {
    transform: scale(1) rotate(0)
  }
  100% {
    transform: scale(1) rotate(0)
  }
}

.callToAction {
  animation: tada_9122 10s linear infinite;
  animation-delay: 1s;
  transform-origin: 50% 50%;
}

.callToAction:hover {
  animation: tada_9122_2 10s linear infinite;
  transform-origin: 50% 50%;
}
<div class="callToAction" style="width:100px;height:100px;background-color:red;">

</div>

关于html - css animation+transition 每 10 秒触发一次并且在悬停时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44225300/

相关文章:

CSS float div 在窗口调整大小时重叠。我该如何阻止呢?

css - 如何在CSS中使用 float 创建表格布局

java - dom4j HTMLWriter 缺少 XML 声明

javascript - jQuery 检测已经渲染的元素?

javascript - 当我在 iOS 网站上输入密码时如何隐藏最后一位数字

javascript - javascript初学者,幻灯片无法运行

jquery - 当 iframe 及其 CSS 加载和渲染时,如何触发 jQuery 警报?

html - 使用 CSS 在浏览器调整大小时调整 div 的宽度和高度

html - 为什么我的 div 吞下了它之前的那个?

悬停时运行的 CSS3 关键帧动画