forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover.js
More file actions
executable file
·57 lines (47 loc) · 1.69 KB
/
hover.js
File metadata and controls
executable file
·57 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// add/remove .hover onmouseenter/leave
// for mobile devices (:hover sticks)
let currentHoverElem;
/*
function log(e) {
console.log(Date.now() % 1e4, e.type);
}
document.addEventListener("focusin", log, false);
document.addEventListener("focus", log, false);
document.addEventListener("touchstart", log, false);
document.addEventListener("touchend", log, false);
document.addEventListener("touchcancel", log, false);
document.addEventListener("touchleave", log, false);
document.addEventListener("touchmove", log, false);
document.addEventListener("touch", log, false);
document.addEventListener("pointerup", log, false);
document.addEventListener("pointerdown", log, false);
document.addEventListener("pointermove", log, false);
document.addEventListener("pointercancel", log, false);
document.addEventListener("mouseover", log, false);
*/
document.addEventListener('mouseover', function(event) {
let target = event.target.closest('[data-add-class-on-hover]') || event.target.closest('.button');
if (target) {
currentHoverElem = target;
target.classList.add('hover');
}
});
document.addEventListener('touchend', function(event) {
setTimeout(function() {
if (currentHoverElem) {
currentHoverElem.classList.remove('hover');
currentHoverElem = null;
}
}, 500); // touchstart -> tourchend -> (delay up to 300ms) -> mouseover
});
document.addEventListener('mouseout', function(event) {
if (!currentHoverElem) return;
if (currentHoverElem.contains(event.relatedTarget)) {
return;
}
currentHoverElem.classList.remove('hover');
currentHoverElem = null;
});
if (!navigator.userAgent.match(/(iPad|iPhone|iPod)/g)) {
document.documentElement.classList.add('working-hover');
}