简体   繁体   English

如何确保我的事件始终在jQuery中触发? [悬停回调未触发]

[英]How can I make sure my events always fire in jQuery? [Hover callback not firing]

I have a BG image animation that relies on the hover callback to revert to it's original state. 我有一个BG图像动画,它依赖于hover回调来恢复到其原始状态。 If I move the mouse quickly over the links, the hovered state sticks. 如果我将鼠标快速移到链接上,则将停留在悬停状态。 My guess is that I'm moving the mouse off before the first animation completes, so the callback doesn't register. 我的猜测是我要在第一个动画完成之前将鼠标移开,因此回调不会注册。

Is there a more bulletproof way to write this function? 有没有更防弹的方式来编写此功能?

$('.nav li a').hover(function() {
    $(this).addClass('hovered', 300);
}, function() {
    $(this).removeClass('hovered', 300);
});

(it uses a BGimg plugin to support the speed parameter on add/removeClass) (它使用BGimg插件来支持add / removeClass上的speed参数)

Testable here: McPherson Industries 可在此处测试: McPherson Industries

This is a common problem and cannot be fixed, I'm afraid – it's up to the browser to keep up with events, and if it can't, well, you're out of luck. 恐怕这是一个普遍的问题,无法解决–取决于浏览器来跟上事件的发生,如果无法解决问题,那说明您不走运。

It might be jQuery's fault also, though. 不过,这可能也是jQuery的错。 Hard to say without dwelling into the source code. 不用讲源代码就很难说。 Anyways, there's a easy workaround: 无论如何,有一个简单的解决方法:

$('.nav li a').hover(function() {
    // When element is hovered, remove hovered state from all elements..
    $('.hovered').removeClass('hovered');
    // ..and add it to this one.
    $(this).addClass('hovered', 300);
}, function() {
    // This is only needed when exiting an element and not enterin another one
    $(this).removeClass('hovered', 300);
});

This might not be 100% certain, but it's better than your current. 这可能不是100%肯定的,但是比您当前的要好。

What is the 300 value in the addClass method ? addClass方法的300值是多少? it does not take two parameters.. just one 它不需要两个参数。
Api reference: http://api.jquery.com/addClass/ ( ok,noticed the plugin ) api参考: http : //api.jquery.com/addClass/ 确定,请注意插件

It works just fine for me .. all links revert back to their original image, no-matter how fast i move the mouse.. 它对我来说很好用..所有链接都恢复为原始图像,没关系,我移动鼠标有多快。

Update 更新资料

Ok i see the problem now.. 好吧,我现在看到了问题。

  • Chrome is OK ( the only one it seems.. ) Chrome可以( 似乎是唯一的一个..
  • Firefox shows the problem.. Firefox显示了问题。
  • IE shows the problem IE显示问题

Update 2 It seems to me that the problem is with the plugin you use.. It ignores queued requests to the same object until the current animation has finished.. So if you hover out before the first one has completed it will not return to normal.. 更新2在我看来,问题出在您所使用的插件上。它会忽略对同一对象的排队请求,直到当前动画完成为止。因此,如果您在第一个动画完成之前将鼠标悬停,它将无法恢复正常..

I can't help more on this, sorry .. 我对此无能为力,对不起..

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

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