简体   繁体   English

jQuery - 悬停时淡化动画

[英]jQuery - Fading animation on hover

I have a little jQuery animation which fades in a link when hovering an : 我有一个小jQuery动画,当悬停时,它会在链接中淡入:

$(function() {
  $('.delete').hide();
  $('#photos img').hover(function() {
    $(this).parents('li').children('.delete').fadeIn('fast');
  }, function() {
    $(this).parents('li').children('.delete').fadeOut('fast');
  });
});

But if I quickly move my mouse in and out of the image, the new animation is always added to the queue and when I stop I can see the link pulsating for a while. 但是,如果我快速将鼠标移入和移出图像,新动画将始终添加到队列中,当我停止时,我可以看到链接脉动一段时间。 I tried using .stop(true), but then sometimes the fade in effect doesn't work at all (or just up to some opacity value less than 1). 我尝试使用.stop(true),但有时淡入效果根本不起作用(或者只是一些不透明度值小于1)。 What can I do? 我能做什么?

Thanks, Eric 谢谢,埃里克

The best way is to use hoverIntent plugin. 最好的方法是使用hoverIntent插件。 This deals with the issues above. 这涉及上述问题。 It also adds a slight delay to the animation so if a user happens to quickly move the mouse over all the links you do not get an ugly animation flow of all the links. 它还会为动画添加一点延迟,因此如果用户碰巧在所有链接上快速移动鼠标,则不会获得所有链接的丑陋动画流。

One way to prevent such problems occuring is to use stop() in conjunction with fadeTo(), as in the snippet below: 防止出现此类问题的一种方法是将stop()与fadeTo()结合使用,如下面的代码段所示:

$(function() {
  $('.delete').fadeTo(0, 0);
  $('#photos img').hover(function() {
    $(this).parents('li').children('.delete').stop().fadeTo('fast', 1);
  }, function() {
    $(this).parents('li').children('.delete').stop().fadeTo('fast', 0);
  });
});

Hope this solves your issue! 希望这能解决您的问题!

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

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