jQuery淡出闪烁

前端之家收集整理的这篇文章主要介绍了jQuery淡出闪烁前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有jQuery fade在这里: http://dougie.thewestharbour.com/

当你将鼠标悬停在.main-overlay div上时,我希望它会淡出然后当你将鼠标从它上面移开时,我希望它能够淡入.

但是,你可以看到它现在只是闪烁.我猜这是因为div消失所以当它消失时它被视为鼠标输出,但我不知道如何去解决它.

这是我的javascript:

  1. $(document).ready(function () {
  2.  
  3.  
  4. $('.main-overlay').hover(
  5.  
  6. //MouSEOver,fadeIn the hidden hover class
  7. function() {
  8.  
  9. $(this).fadeOut('1000');
  10.  
  11. },//MouSEOut,fadeOut the hover class
  12. function() {
  13.  
  14. $(this).fadeIn('1000');
  15.  
  16. }).click (function () {
  17.  
  18. //Add selected class if user clicked on it
  19. $(this).addClass('selected');
  20.  
  21. });
  22.  
  23. });

这是叠加div附加到的项目之一:

  1. <li><div class="main-overlay"></div><span class="caption">The store front</span><img src="http://dougie.thewestharbour.com/wp-content/uploads/store-front.jpg" alt="store front" title="store-front" width="730" height="360" class="alignnone size-full wp-image-98" /></li>

谢谢,

解决方法

之所以发生这种情况是因为fadeOut的结尾处有一个显示:none,所以当你在fadeOut完成后移动你的鼠标时,它会触发unhover功能.相反,只需为不透明度设置动画:
  1. $('.main-overlay').hover(function() {
  2. $(this).animate({opacity: 0},1000);
  3. },function() {
  4. $(this).animate({opacity: 1},1000);
  5. })

Example →

猜你在找的jQuery相关文章