jQuery - unbind or rebind hoverIntent()?

前端 未结 4 1998
小蘑菇
小蘑菇 2020-12-14 23:33

I have a menu bar that displays a set of categories in an upper row.

One of the categories has a set of sub-categories.

I have a hoverIntent setup so that it

4条回答
  •  既然无缘
    2020-12-14 23:56

    I think this is a more complete answer. It does the following:

    • Any active timer is cleaned up.
    • All events are cleared
    • All object properties are cleared
    • Uses common jQuery syntax and looks like native part of hoverIntent

    Code:

    (function($) {
       if (typeof $.fn.hoverIntent === 'undefined')
         return;
    
       var rawIntent = $.fn.hoverIntent;
    
       $.fn.hoverIntent = function(handlerIn,handlerOut,selector) 
        {
          // If called with empty parameter list, disable hoverintent.
          if (typeof handlerIn === 'undefined')
          {
            // Destroy the time if it is present.
            if (typeof this.hoverIntent_t !== 'undefined') 
            { 
              this.hoverIntent_t = clearTimeout(this.hoverIntent_t); 
            }
            // Cleanup all hoverIntent properties on the object.
            delete this.hoverIntent_t;
            delete this.hoverIntent_s;
    
            // Unbind all of the hoverIntent event handlers.
            this.off('mousemove.hoverIntent,mouseenter.hoverIntent,mouseleave.hoverIntent');
    
            return this;
          }  
    
          return rawIntent.apply(this, arguments);
        };  
    })(jQuery);
    

提交回复
热议问题