Skip to content

Commit d8567ac

Browse files
committed
Added focus/blur handling as an option
1 parent 851c869 commit d8567ac

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

dist/hoverintent.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
<div class='fixed-700'>
1111
<p class='intro'>Sometimes you want to limit when a mouse event is fired if a user has unintentionaly hovered over an element. <a href='https://github.com/tristen/hoverintent'>hoverintent</a> tracks the sensitivity of a mouse position and fires an event within a threshold. You can also pass additional options that adjusts when an event is triggered. <a href='https://raw.github.com/tristen/hoverintent/gh-pages/dist/hoverintent.min.js'>Download the source</a> and read its <a href='https://github.com/tristen/hoverintent#hoverintent'>usage on GitHub.</a></p>
1212
<ul class='examples'>
13-
<li id='first'>
13+
<li id='first' tabindex="0">
1414
Plain old hover
1515
<span class='popup'>Hi there</span>
1616
</li>
17-
<li id='second'>
17+
<li id='second' tabindex="0">
1818
hoverintent
1919
<span class='popup'>Hi there</span>
2020
</li>
21-
<li id='third'>
21+
<li id='third' tabindex="0">
2222
Custom settings
2323
<span class='popup'>Hi there</span>
2424
</li>
@@ -64,7 +64,8 @@
6464
}, function(e) {
6565
this.className = 'off';
6666
}).options({
67-
timeout:500
67+
timeout:500,
68+
handleFocus: true
6869
});
6970

7071
var analytics=analytics||[];(function(){var e=["identify","track","trackLink","trackForm","trackClick","trackSubmit","page","pageview","ab","alias","ready","group"],t=function(e){return function(){analytics.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var n=0;n<e.length;n++)analytics[e[n]]=t(e[n])})(),analytics.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d2dq2ahtl5zl1z.cloudfront.net/analytics.js/v1/"+e+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)};

index.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
module.exports = function(el, onOver, onOut) {
44
var x, y, pX, pY;
5+
var mouseOver = false;
56
var h = {},
67
state = 0,
78
timer = 0;
89

910
var options = {
1011
sensitivity: 7,
1112
interval: 100,
12-
timeout: 0
13+
timeout: 0,
14+
handleFocus: false
1315
};
1416

1517
function delay(el, e) {
@@ -39,11 +41,16 @@ module.exports = function(el, onOver, onOut) {
3941

4042
// Public methods
4143
h.options = function(opt) {
44+
var focusOptionChanged = opt.handleFocus !== options.handleFocus;
4245
options = Object.assign({}, options, opt);
46+
if (focusOptionChanged) {
47+
options.handleFocus ? addFocus() : removeFocus();
48+
}
4349
return h;
4450
};
4551

4652
function dispatchOver(e) {
53+
mouseOver = true;
4754
if (timer) timer = clearTimeout(timer);
4855
el.removeEventListener('mousemove', tracker, false);
4956

@@ -62,6 +69,7 @@ module.exports = function(el, onOver, onOut) {
6269
}
6370

6471
function dispatchOut(e) {
72+
mouseOver = false;
6573
if (timer) timer = clearTimeout(timer);
6674
el.removeEventListener('mousemove', tracker, false);
6775

@@ -74,10 +82,33 @@ module.exports = function(el, onOver, onOut) {
7482
return this;
7583
}
7684

85+
function dispatchFocus(e) {
86+
if (!mouseOver) {
87+
onOver.call(el, e);
88+
}
89+
}
90+
91+
function dispatchBlur(e) {
92+
if (!mouseOver) {
93+
onOut.call(el, e);
94+
}
95+
}
96+
97+
function addFocus() {
98+
el.addEventListener('focus', dispatchFocus, false);
99+
el.addEventListener('blur', dispatchBlur, false);
100+
}
101+
102+
function removeFocus() {
103+
el.removeEventListener('focus', dispatchFocus, false);
104+
el.removeEventListener('blur', dispatchBlur, false);
105+
}
106+
77107
h.remove = function() {
78108
if (!el) return;
79109
el.removeEventListener('mouseover', dispatchOver, false);
80110
el.removeEventListener('mouseout', dispatchOut, false);
111+
removeFocus();
81112
};
82113

83114
if (el) {

0 commit comments

Comments
 (0)