Finnnn
Finnnn

Reputation: 3580

Jquery - fadeIn/fadeOut flicker on rollover

I am using the following code to acheive a fadeIn/fadeOut effect on rollover/rollout of it's parent div.

$('.rollover-section').hover(function(){  
   $('.target', this).stop().fadeIn(250)
 }, function() {
   $('.target', this).stop().fadeOut(250)
})

It works correctly when I rollover the div and out slowly. However if I move my mouse over and then off the div quickly, it breaks the effect. The target div seems to get stuck at an opacity between 0 and 1.

What confuses me is that when I use the following code it works perfectly.

$('.rollover-section').hover(function(){  
  $('.target', this).stop().animate({
      opacity: 1
    }, 250);
}, function() {
  $('.target', this).stop().animate({
      opacity:0 
    }, 250);
})

So, I have two questions.

1 - why is my first code block behaving as it does?

2 - What is the difference between fadeIn()/fadeOut() and animating the opacity?

Upvotes: 2

Views: 2823

Answers (2)

Luwe
Luwe

Reputation: 3034

I've put my answer from the comments here:

Just use the animate example you have there. Check here for an answer to why the fade animation behaves the way it does: jQuery fade flickers

Upvotes: 0

Matt
Matt

Reputation: 7249

As it was stated already it's because those modify the css and change the display to none. By using fadeTo you can get the same effect, but it doesn't modify the css, so it should work correctly.

update example: http://jsfiddle.net/TFhzE/1/

you can also do

$('.rollover-section').hover(function() {  
   $('.target', this).stop().fadeTo(0,250);
 }, function() {
   $('.target', this).stop().fadeTo(250,0,function(){$(this).hide();});
});

to completely hide it yourself once it actually is complete.

Upvotes: 1

Related Questions