Demonstration how to support hover effects on both mouse and touch devices, by only binding to the proper events.
No browser- or media detection (so it works on devices with both mouse and touch!), no preventDefault() or clumsy simulation of click events is necessary.
<p>Demonstration of hover effects with mouse and touch. No browser- or media detection (so it works on devices with both mouse and touch!), no preventDefault() or clumsy simulation of click events is necessary.</p>
<p>See <a href="http://www.macfreek.nl/memory/Touch_and_mouse_with_hover_effects_in_a_web_browser">http://www.macfreek.nl/memory/Touch_and_mouse_with_hover_effects_in_a_web_browser</a>.</p>
<p>Just:</p>
<ul>
<li>Add hover effects on <code>touchstart</code> and <code>mouseenter</code>.</li>
<li>Remove hover effects on <code>mouseleave</code>, <code>touchmove</code> and <code>click</code>.</li>
<li>Ignore all those <code>touchend</code> events!</li>
</ul>
<div class="tests">
<a href="http://stackoverflow.com/" id="pure_css" class="test">1. regular :hover effect with CSS<div class="content">more info</div></a>
<a href="http://stackoverflow.com/" id="regular_js" class="test">2. JavaScript-based hover<div class="content">more info</div></a>
<a href="http://stackoverflow.com/" id="show_on_touch" class="test">3. JavaScript-based hover as #2 with highlight on touchstart<div class="content">more info</div></a>
<a href="http://stackoverflow.com/" id="support_cancel" class="test">4. JavaScript-based hover as #3 with reset on touchmove<div class="content">more info</div></a>
</div>
<p>Explanation:</p>
<ol>
<li>This is a regular hover effect using CSS. Note that it is possible to show and hide content by hovering using pure CSS. On a touchpad, you will have to click this link twice in order to follow it. This is because a content change on a mouseover will prevent the click event from firing.</li>
<li>This is the very some hover effect, only implemented in JavaScript. On a touchpad, you still have to click this link twice in order to follow it.</li>
<li>This time, the hover effect is already started on touchstart. First, this make the hover faster on touch. Secondly, the mouseover event does not change the state (the state has...