briancherne / jquery-hoverIntent

hoverIntent jQuery Plug-in
https://briancherne.github.io/jquery-hoverIntent/
MIT License
826 stars 252 forks source link

Is it possible to attach the "out" function to a different selector? #23

Closed gkatsanos closed 9 years ago

gkatsanos commented 10 years ago

I have a mega drop-down menu, which doesn't follow the traditional HTML structure of dropdowns... (the dropdown is not a child of the <li> menu item.)

Here's how my html looks like:

<nav>
<ul class="menu">
<li class="menu-item">
Menu item 1
</li>
</ul>
</nav>
<section id="content">
<div class="dropdown">
<p>my dropdown content is here</p>
</div>

I don't know if you see the problem; I need to have an event handler on the "dropdow" class separately...

Now my code looks like:

      jQuery('.menu-item[data-mlid]').hoverIntent({
        over: hoverIn,
        out: hoverOut,
        sensitivity: 1,
        timeout: 200,
      });
      function hoverIn() {
        var activeitem = $(this).data("mlid");
        $('.paddle-mega-dropdown[data-mlid=' + activeitem + ']').fadeIn("fast");
      }
      // normally the mouseleave event should be handled by ther hoverIntent script
      // but in our case we need to handle it ourselves
      function hoverOut() {
        $('.menu-item[data-mlid], .paddle-mega-dropdown').mouseleave(function(e) {
          var activeitem = $(this).data("mlid");
          var targetElement = e.relatedTarget;
          if ($(targetElement).closest('[data-mlid=' + activeitem + ']').length == 0) {
            $('.paddle-mega-dropdown[data-mlid=' + activeitem + ']').fadeOut("fast");
          }
        });
      }

but I guess that's not how the plugin is supposed to work..

usmonster commented 9 years ago

Hi @gkatsanos! Thanks for asking. For your case, would you just be able to, in hoverIn, just add a reference to the menu item as data on the dropdown content, use $.noop for out, and just register a listener for mouseleave on the content? For example, something like this:

$('.menu-item[data-mlid]').hoverIntent({
  over: hoverIn,
  out: $.noop,
  sensitivity: 1,
  timeout: 200
});
function hoverIn() {
  var activeitem = $(this).data('mlid');
  $('.paddle-mega-dropdown[data-mlid="' + activeitem + '"]').fadeIn('fast');
}
// only handle mouseleave of content
$('.paddle-mega-dropdown').mouseleave(function(e) {
  var activeitem = $(this).data('mlid');
  var targetElement = e.relatedTarget;
  if (! $(targetElement).closest('[data-mlid="' + activeitem + '"]').length) {
    $('.paddle-mega-dropdown[data-mlid="' + activeitem + '"]').fadeOut('fast');
  }
});

? Please let me know if this addresses your concern or if you've found another way already, and I will close the issue.

gkatsanos commented 9 years ago

As I found a way to do this already, I close my issue. Many thanks.

andreyvolokitin commented 9 years ago

@gkatsanos, interesting to see your solution.

joenammour commented 8 years ago

I found a way to do this if anyone is still looking for a resolution. Basically you can use the $.noop jquery empty function for either the over or out function. Then you can just call hoverIntent as two different functions like this.

$("#main-navigation").hoverIntent( menuHoverOver, $.noop, 'li a' );
$("#main-navigation").hoverIntent( $.noop, menuHoverOut);