<h1>Draft <code>:hover</code> Detection</h1>
<p>Open your console, please! Then refresh! Then mouseover the "Result" panel.</p>
<p>In this case, the only detection is <em>deferred</em> detection. You cannot detect :hover without a human. We minimize this flaw by firing the detection as soon as a mouse-owning user moves the cursor.</p>
<p>This works, if not as seamlessly as some other tests.</p>
CSS
h1 {
font-size: larger;
}
p {
margin: 15px;
}
em {
font-style: italic;
}
JavaScript
var elRoot = document.documentElement,
elStyle = document.createElement('style');
elStyle.innerHTML = 'html:hover { clear: both; }';
elRoot.getElementsByTagName('head')[0].appendChild(elStyle);
function detectHover() {
elRoot.removeEventListener('mousemove', detectHover, false); // The event is gone after the first mousemove!!
if(elRoot.currentStyle) {
var rootHoverStyle = elRoot.currentStyle.clear;
} else {
var rootHoverStyle = document.defaultView.getComputedStyle(elRoot,null).getPropertyValue('clear');
}
// IF THIS IS TRUE YOU HAVE SOME HOVER
console.log(rootHoverStyle === 'both');
elStyle.parentNode.removeChild(elStyle);
}
elRoot.addEventListener('mousemove', detectHover, false);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.