简体   繁体   English

jQuery,css3动画和计时问题

[英]Problems with jquery, css3 animations and timing

I have a couple of images which should perform a short animation upon hover. 我有几张图像,在悬停时应执行简短的动画处理。

This is what I'm using to give them the animation class on hover, and removing it after the animation time is over. 这就是我用来为他们提供悬停时的动画类,并在动画时间结束后将其删除的原因。

$('.box').on('mouseenter',function(){
    var box = $(this);

    $(box).addClass('swing');

    setTimeout(function(){
      $(boxen).removeClass('swing');
    },2000);

});

Problem is, it's quite buggy. 问题是,这很容易出错。 Check out http://jsbin.com/onemix and hover them a few times and you'll see the problem. 查看http://jsbin.com/onemix并将其悬停几次,您将看到问题。

I think I need to check if they're animating when the hover triggers, to avoid a new timeout going of. 我认为我需要检查悬停触发时它们是否在动画,以免发生新的超时。

Any suggestions? 有什么建议么?

You could check for the swing class beeing present using hasClass() (note that i've also removed the unnecessary double initialization): 您可以使用hasClass()检查swing类是否存在(注意,我还删除了不必要的双重初始化):

http://jsbin.com/onemix/8 http://jsbin.com/onemix/8

Also it might be better not to use setTimeout, but the appropriate animation end event: 另外,最好不要使用setTimeout,而是使用适当的动画结束事件:

http://jsbin.com/onemix/9 http://jsbin.com/onemix/9

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

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