Skip to content

Instantly share code, notes, and snippets.

@grandadmiral-thrawn
Created November 25, 2017 23:01
Show Gist options
  • Save grandadmiral-thrawn/6f94b585e230282c05e42de2c0306971 to your computer and use it in GitHub Desktop.
Save grandadmiral-thrawn/6f94b585e230282c05e42de2c0306971 to your computer and use it in GitHub Desktop.
detect hovering in js
(function() {
var matchfunc = null, prefixes = ["","ms","moz","webkit","o"], i, m;
for(i=0; i<prefixes.length; i++) {
m = prefixes[i]+(prefixes[i] ? "Matches" : "matches");
if( document.documentElement[m]) {matchfunc = m; break;}
m += "Selector";
if( document.documentElement[m]) {matchfunc = m; break;}
}
if( matchfunc) window.isHover = function(elem) {return elem[matchfunc](":hover");};
else {
window.onmouseover = function(e) {
e = e || window.event;
var t = e.srcElement || e.target;
while(t) {
t.hovering = true;
t = t.parentNode;
}
};
window.onmouseout = function(e) {
e = e || window.event;
var t = e.srcElement || e.target;
while(t) {
t.hovering = false;
t = t.parentNode;
}
};
window.isHover = function(elem) {return elem.hovering;};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment