javascript - 告诉 css 动画结束的 js 警报

标签 javascript jquery html css

我有一个盒子并应用了一个旋转 css 动画,我想添加一个 js 函数,在动画结束时给我一个警报,我如何使用 webkitTransitionEnd 做到这一点?

这是我的代码:

.box {
  background: red;
  position: absolute;
  padding: 100px;
}
.box:hover {
  animation-name: rotate;
  animation-duration: 2s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<html>
<body>
  <div class="box">
  </div>
</body>
</html>

最佳答案

使用 css 实际上是不可能的,但是因为你的动画是 infinite 并且在 hover 事件上触发,你可以监听 mouseleave 事件(当元素不再悬停时动画停止)。

$('.box').mouseleave(function() {
  console.log('animation has ended');
});
.box {
  background: red;
  position: absolute;
  padding: 100px;
}

.box:hover {
    animation-name: rotate; 
    animation-duration: 2s; 
    animation-iteration-count: infinite;
    animation-timing-function: linear;
}

@keyframes rotate {
    from {transform: rotate(0deg);}
    to {transform: rotate(360deg);}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box"></div>

关于javascript - 告诉 css 动画结束的 js 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217201/

相关文章:

javascript - Fabric js 图像过滤器加载问题

javascript - 调用 Bootstrap 函数时为 "Uncaught TypeError: undefined is not a function"

jQuery - 使用 id 添加/删除类时组合多个单击功能

javascript - 在提交表单之前强制更改隐藏字段的值

html - Excel VBA 为文本抓取 HTML 表格

javascript - 如何在javascript中将字符串解析为int

javascript - NetSuite:在 SUITESCRIPT 2.0 中使用 SUITESCRIPT 1.0

jQuery :last or :lastChild and how?

php - 使用 AJAX/PHP 将其他网站表格中的内容插入到我的表单中?

javascript - 旧版 HTML - 带有文本值的文本链接表单提交