views:

335

answers:

2

This is a fairly easy one, but I cant seem to figure it out.

Basically I have a jquery hover that fades in a div and fades out the other upon hover.

When I quickly hover on and off a few times it pulses back and forth for about 3-4 seconds to finish all those fade in/fade outs.

I generally stop these things with .stop(), but it doesnt seem to do the trick here. How can I kill the fade in if I hover off the button before the an`$(".txtWrap").stop().hover(

$(".txtWrap").stop().hover(
  function () {
    $(this).find('.txtBock').fadeOut();
    $(this).find('.txtDesc').fadeIn();

  },
  function () {
    $(this).find('.txtBock').fadeIn();
    $(this).find('.txtDesc').fadeOut();
  }
)
+4  A: 

Your stop() is misplaced.

Try this:

$(".txtWrap").hover(
  function () {
    $(this).find('.txtBock').stop().fadeOut();
    $(this).find('.txtDesc').fadeIn();
  //  $('#timeTxt').fadeOut();
  //  $('#timeTxtDesc').fadeIn();

  },
  function () {
    $(this).find('.txtBock').fadeIn();
    $(this).find('.txtDesc').stop().fadeOut();
  }
)

EDIT:

This will animate the elements' opacity without hiding the element. If you want them hidden, use .hide() you'll need to add a callback to the animate function.

$(".txtWrap").hover(
  function () {
    $(this).find('.txtBock').stop().animate({opacity:0}, 500);
    $(this).find('.txtDesc').animate({opacity:1}, 500);
  //  $('#timeTxt').fadeOut();
  //  $('#timeTxtDesc').fadeIn();

  },
  function () {
    $(this).find('.txtBock').animate({opacity:1}, 500);
    $(this).find('.txtDesc').stop().animate({opacity:0}, 500);
  }
)
patrick dw
I tried that works on the first off-hover, but then I can never get .txtDesc to hover back in..
Wes
@Wes - I removed the `stop()` from the fade in. If the `txtWrap` is sized based on its content, then likely when one element faded out, and was hidden, the `txtWrap` resized and moved out from under the mouse pointer triggering an undesired `mouseleave`. This should ensure that the `fadein` always happens.
patrick dw
That seems to be a bit closer, but when I mouse out prematurely, i'm left with a div with half transparency. So If I mouse out at 50% opacity, when I mouse in again, it will only fade into 50%. Is there a way to reset it?
Wes
I personally tend to not use some of those type of functions for that reason. You may have better success with `.animate()`. Then also, if you don't want the faded elements to be hidden, you don't have to. I'll update my answer.
patrick dw
Great. Thats a good ideas. Here is my final code:$('.txtDesc').animate({opacity:0}, 1);$(".txtWrap").hover( function () { $(this).find('.txtBlock').stop().animate({opacity:0}, 500); $(this).find('.txtDesc').show().animate({opacity:1}, 500); // $('#timeTxt').fadeOut(); // $('#timeTxtDesc').fadeIn(); }, function () { $(this).find('.txtBlock').animate({opacity:1}, 500); $(this).find('.txtDesc').stop().animate({opacity:0}, 500); })
Wes
@Wes - Looks good. For the first line, you might as well use `css()` instead of `animate()`. `$('.txtDesc').css({opacity:0});`
patrick dw
A: 

In times like this I turn to Brian Cherne's genius .hoverIntent() plugin:
http://bit.ly/hoverintent -- It's quite smooth...waits until the user has slowed down enough before executing. You can configure it to your needs.

Just include the plugin (it's short enough I'll sometimes place it directly into my script file) then add the word Intent:

$(".txtWrap").hoverIntent(
  function () {
    $(this).find('.txtBock').fadeOut();
    $(this).find('.txtDesc').fadeIn();

  },
  function () {
    $(this).find('.txtBock').fadeIn();
    $(this).find('.txtDesc').fadeOut();
  }
)
brandonjp