jquery-hover-intent.js (2063B)
1 /* 2 * Based on tristen hoverintent plugin - https://github.com/tristen/hoverintent 3 */ 4 5 (function( $ ) { 6 'use strict'; 7 8 var hoverIntent = function( el, onOver, onOut ) { 9 var x, y, pX, pY, 10 h = {}, 11 state = 0, 12 timer = 0; 13 14 var options = { 15 sensitivity: 7, 16 interval: 100, 17 timeout: 0 18 }; 19 20 function delay( el, e ) { 21 if ( timer ) 22 timer = clearTimeout( timer ); 23 24 state = 0; 25 26 return onOut ? onOut.call( el, e ) : null; 27 } 28 29 function tracker( e ) { 30 x = e.clientX; 31 y = e.clientY; 32 } 33 34 function compare( el, e ) { 35 if ( timer ) 36 timer = clearTimeout( timer ); 37 38 if ( (Math.abs( pX - x ) + Math.abs( pY - y )) < options.sensitivity ) { 39 state = 1; 40 41 return onOver ? onOver.call( el, e ) : null; 42 } else { 43 pX = x; 44 pY = y; 45 46 timer = setTimeout( function() { 47 compare( el, e ); 48 }, options.interval ); 49 } 50 } 51 52 // Public methods 53 h.options = function( opt ) { 54 options = $.extend( {}, options, opt ); 55 56 return h; 57 }; 58 59 function dispatchOver( e ) { 60 if ( timer ) 61 timer = clearTimeout( timer ); 62 63 el.removeEventListener( 'mousemove', tracker ); 64 65 if ( state !== 1 ) { 66 pX = e.clientX; 67 pY = e.clientY; 68 69 el.addEventListener( 'mousemove', tracker ); 70 71 timer = setTimeout( function() { 72 compare( el, e ); 73 }, options.interval ); 74 } 75 76 return this; 77 } 78 79 function dispatchOut( e ) { 80 if ( timer ) 81 timer = clearTimeout( timer ); 82 83 el.removeEventListener( 'mousemove', tracker ); 84 85 if ( state === 1 ) { 86 timer = setTimeout( function() { 87 delay( el, e ); 88 }, options.timeout ); 89 } 90 91 return this; 92 } 93 94 h.remove = function() { 95 el.removeEventListener( 'mouseover', dispatchOver ); 96 el.removeEventListener( 'mouseleave', dispatchOut ); 97 }; 98 99 el.addEventListener( 'mouseover', dispatchOver ); 100 101 el.addEventListener( 'mouseleave', dispatchOut ); 102 103 return h; 104 }; 105 106 $.fn.hoverIntent = function( over, out, options ) { 107 return this.each( function() { 108 hoverIntent( this, over, out ).options( options || {} ); 109 } ); 110 }; 111 112 })( jQuery );