Created
November 25, 2017 23:01
-
-
Save grandadmiral-thrawn/6f94b585e230282c05e42de2c0306971 to your computer and use it in GitHub Desktop.
detect hovering in js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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