tags:

views:

1662

answers:

1

I put this code together from a snippet from somewhere else, so I may not be doing this very well. I have multiple div's that looks like this:

<div class="services">
<p>...</p>
<ol class="serviceList" style="display: none;">
  <li>
    <p>service</p>
    <ul>
      <li>service description</li>
    </ul>
  </li>
...
</ol>
</div>

I want the <ol> to slideDown when the services div is hovered over. It needs a delay so that it doesn't vanish if the user mistakenly mouseout. BUT if they mouse over another services div then the visible one needs to go away immediately to make room for the new serviceList Here is what I have now:

$('.services').each(function () {
            var time = 800;
      var hideDelay = 1000;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
      var trigger = $(this);
      var info = $('.serviceList', this);

      $([trigger.get(0),info.get(0)]).mouseover(function () {


                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again

                    return;
                } else {



      info.addClass('active');

                    // reset position of info box
                    beingShown = true;

                    info.css('zindex','2')
        .slideDown(time, function() {
                        beingShown = false;
                        shown = true;
                    });
                }

                return false;
            }).mouseout(function () {

                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;

                    info.css('zindex','0')
        .fadeOut( hideDelay, function () {
                        shown = false;
                        info.removeClass('active');
                    });


                }, hideDelay);

                return false;
            });


     });

I have a poor understanding of scope so I don't know what the get(0) stuff is about. I've been trying to have an event on mouseover that checks if any other "serviceList" is .active and then hide it. That works, unless you switch between "services" too fast. Am even close to doing this right?

Thanks

+5  A: 

You should try using the hoverIntent plugin. This is what it is designed for.

$('.services').hoverIntent(
     function() { // over
        $(this).find('.serviceList:first').slideDown();
     },
     function() { // out
        $(this).find('.serviceList:first').slideUp();
     }
);

Note that this is basically the same as the hover() method, but it includes the delay that you are looking for.

tvanfosson
Note: I changed the traversal method as the list is a child of the DIV not a sibling.
tvanfosson
+1, hoverintent is the best way to do this
karim79
This worked very well. Thanks
DrGlass