|
1 |
| -import $ from 'jquery'; |
2 |
| - |
3 | 1 | /**
|
4 | 2 | * hoverIntent is similar to jQuery's built-in "hover" function except that
|
5 | 3 | * instead of firing the onMouseOver event immediately, hoverIntent checks
|
@@ -29,92 +27,94 @@ import $ from 'jquery';
|
29 | 27 | * @param g onMouseOut function || Nothing (use configuration options object)
|
30 | 28 | * @author Brian Cherne brian(at)cherne(dot)net
|
31 | 29 | */
|
32 |
| -$.fn.hoverIntent = function (f, g) { |
33 |
| - // default configuration options |
34 |
| - let cfg = { |
35 |
| - sensitivity: 7, |
36 |
| - interval: 100, |
37 |
| - timeout: 0, |
38 |
| - }; |
39 |
| - // override configuration options with user supplied object |
40 |
| - cfg = $.extend(cfg, g ? { over: f, out: g } : f); |
| 30 | + (function ($) { |
| 31 | + $.fn.hoverIntent = function (f, g) { |
| 32 | + // default configuration options |
| 33 | + var cfg = { |
| 34 | + sensitivity: 7, |
| 35 | + interval: 100, |
| 36 | + timeout: 0, |
| 37 | + }; |
| 38 | + // override configuration options with user supplied object |
| 39 | + cfg = $.extend(cfg, g ? { over: f, out: g } : f); |
41 | 40 |
|
42 |
| - // instantiate variables |
43 |
| - // cX, cY = current X and Y position of mouse, updated by mousemove event |
44 |
| - // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval |
45 |
| - let cX, cY, pX, pY; |
| 41 | + // instantiate variables |
| 42 | + // cX, cY = current X and Y position of mouse, updated by mousemove event |
| 43 | + // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval |
| 44 | + var cX, cY, pX, pY; |
46 | 45 |
|
47 |
| - // A private function for getting mouse position |
48 |
| - let track = function (ev) { |
49 |
| - cX = ev.pageX; |
50 |
| - cY = ev.pageY; |
51 |
| - }; |
52 |
| - |
53 |
| - // A private function for comparing current and previous mouse position |
54 |
| - let compare = function (ev, ob) { |
55 |
| - ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
56 |
| - // compare mouse positions to see if they've crossed the threshold |
57 |
| - if (Math.abs(pX - cX) + Math.abs(pY - cY) < cfg.sensitivity) { |
58 |
| - $(ob).off('mousemove', track); |
59 |
| - // set hoverIntent state to true (so mouseOut can be called) |
60 |
| - ob.hoverIntent_s = 1; |
61 |
| - return cfg.over.apply(ob, [ev]); |
62 |
| - } else { |
63 |
| - // set previous coordinates for next time |
64 |
| - pX = cX; |
65 |
| - pY = cY; |
66 |
| - // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs) |
67 |
| - ob.hoverIntent_t = setTimeout(function () { |
68 |
| - compare(ev, ob); |
69 |
| - }, cfg.interval); |
70 |
| - } |
71 |
| - }; |
| 46 | + // A private function for getting mouse position |
| 47 | + var track = function (ev) { |
| 48 | + cX = ev.pageX; |
| 49 | + cY = ev.pageY; |
| 50 | + }; |
72 | 51 |
|
73 |
| - // A private function for delaying the mouseOut function |
74 |
| - let delay = function (ev, ob) { |
75 |
| - ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
76 |
| - ob.hoverIntent_s = 0; |
77 |
| - return cfg.out.apply(ob, [ev]); |
78 |
| - }; |
79 |
| - |
80 |
| - // A private function for handling mouse 'hovering' |
81 |
| - let handleHover = function (e) { |
82 |
| - // copy objects to be passed into t (required for event object to be passed in IE) |
83 |
| - let ev = $.extend({}, e); |
84 |
| - let ob = this; |
85 |
| - |
86 |
| - // cancel hoverIntent timer if it exists |
87 |
| - if (ob.hoverIntent_t) { |
| 52 | + // A private function for comparing current and previous mouse position |
| 53 | + var compare = function (ev, ob) { |
88 | 54 | ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
|
89 |
| - } |
90 |
| - |
91 |
| - // if e.type == "mouseenter" |
92 |
| - if (e.type == 'mouseenter') { |
93 |
| - // set "previous" X and Y position based on initial entry point |
94 |
| - pX = ev.pageX; |
95 |
| - pY = ev.pageY; |
96 |
| - // update "current" X and Y position based on mousemove |
97 |
| - $(ob).on('mousemove', track); |
98 |
| - // start polling interval (self-calling timeout) to compare mouse coordinates over time |
99 |
| - if (ob.hoverIntent_s != 1) { |
| 55 | + // compare mouse positions to see if they've crossed the threshold |
| 56 | + if (Math.abs(pX - cX) + Math.abs(pY - cY) < cfg.sensitivity) { |
| 57 | + $(ob).off('mousemove', track); |
| 58 | + // set hoverIntent state to true (so mouseOut can be called) |
| 59 | + ob.hoverIntent_s = 1; |
| 60 | + return cfg.over.apply(ob, [ev]); |
| 61 | + } else { |
| 62 | + // set previous coordinates for next time |
| 63 | + pX = cX; |
| 64 | + pY = cY; |
| 65 | + // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs) |
100 | 66 | ob.hoverIntent_t = setTimeout(function () {
|
101 | 67 | compare(ev, ob);
|
102 | 68 | }, cfg.interval);
|
103 | 69 | }
|
| 70 | + }; |
104 | 71 |
|
105 |
| - // else e.type == "mouseleave" |
106 |
| - } else { |
107 |
| - // unbind expensive mousemove event |
108 |
| - $(ob).off('mousemove', track); |
109 |
| - // if hoverIntent state is true, then call the mouseOut function after the specified delay |
110 |
| - if (ob.hoverIntent_s == 1) { |
111 |
| - ob.hoverIntent_t = setTimeout(function () { |
112 |
| - delay(ev, ob); |
113 |
| - }, cfg.timeout); |
| 72 | + // A private function for delaying the mouseOut function |
| 73 | + var delay = function (ev, ob) { |
| 74 | + ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
| 75 | + ob.hoverIntent_s = 0; |
| 76 | + return cfg.out.apply(ob, [ev]); |
| 77 | + }; |
| 78 | + |
| 79 | + // A private function for handling mouse 'hovering' |
| 80 | + var handleHover = function (e) { |
| 81 | + // copy objects to be passed into t (required for event object to be passed in IE) |
| 82 | + var ev = jQuery.extend({}, e); |
| 83 | + var ob = this; |
| 84 | + |
| 85 | + // cancel hoverIntent timer if it exists |
| 86 | + if (ob.hoverIntent_t) { |
| 87 | + ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
114 | 88 | }
|
115 |
| - } |
116 |
| - }; |
117 | 89 |
|
118 |
| - // bind the function to the two event listeners |
119 |
| - return this.on('mouseenter', handleHover).on('mouseleave', handleHover); |
120 |
| -}; |
| 90 | + // if e.type == "mouseenter" |
| 91 | + if (e.type == 'mouseenter') { |
| 92 | + // set "previous" X and Y position based on initial entry point |
| 93 | + pX = ev.pageX; |
| 94 | + pY = ev.pageY; |
| 95 | + // update "current" X and Y position based on mousemove |
| 96 | + $(ob).on('mousemove', track); |
| 97 | + // start polling interval (self-calling timeout) to compare mouse coordinates over time |
| 98 | + if (ob.hoverIntent_s != 1) { |
| 99 | + ob.hoverIntent_t = setTimeout(function () { |
| 100 | + compare(ev, ob); |
| 101 | + }, cfg.interval); |
| 102 | + } |
| 103 | + |
| 104 | + // else e.type == "mouseleave" |
| 105 | + } else { |
| 106 | + // unbind expensive mousemove event |
| 107 | + $(ob).off('mousemove', track); |
| 108 | + // if hoverIntent state is true, then call the mouseOut function after the specified delay |
| 109 | + if (ob.hoverIntent_s == 1) { |
| 110 | + ob.hoverIntent_t = setTimeout(function () { |
| 111 | + delay(ev, ob); |
| 112 | + }, cfg.timeout); |
| 113 | + } |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + // bind the function to the two event listeners |
| 118 | + return this.on('mouseenter', handleHover).on('mouseleave', handleHover); |
| 119 | + }; |
| 120 | +})(jQuery); |
0 commit comments