I came up with the following, which works pretty well - and when switching between touch / pen and mouse.
In essence, to navigate a mouseover mouse, an element βwakes upβ and prepares listeners, then click processed by listeners. For touch navigation, the first press wakes up the element, and the second responder is processed by the listeners.
var hoverCapture = (function() { var TOUCH_STATE = { 'initial': 0, 'over': 1, 'click': 2 }; var eventTargetDefault = '.hover-layer'; function hoverCapture(selectorScope, eventTarget) { var eventTarget = typeof eventTarget !== 'undefined' ? eventTarget : eventTargetDefault; var $eventTarget = $(selectorScope).find(eventTarget); var touchState = TOUCH_STATE.initial; var previousEventType = ''; $eventTarget.on('mouseenter', function(e) { $(this).addClass("hover"); previousEventType = e.type; }); $eventTarget.on('mouseleave', function(e) { $(this).removeClass("hover");
.gallery { margin: 5px; } .image { width: 100px; height: 100px; background: grey; margin: 0 -4px -4px 0; display: inline-block; } .image .overlay { height: 100%; width: 100%; background: red; display: none; position: relative; } .image.hover .overlay { display: block; } .test { width: 25%; height: 25%; position: absolute; right: 0; background: purple; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="gallery" id="gallery1"> <div class="image"> <div class="overlay"> <div class="test"></div> </div> </div> <div class="image"> <div class="overlay"> </div> </div> <div class="image"> <div class="overlay"> </div> </div> <div class="image"> <div class="overlay"> </div> </div> </div> <div class="gallery" id="gallery2"> <div class="image"> <div class="overlay"> <div class="test"> </div> </div> </div> <div class="image"> <div class="overlay"> </div> </div> <div class="image"> <div class="overlay"> <div class="test"> </div> </div> </div> <div class="image"> <div class="overlay"> </div> </div> </div>
If anyone has any add-ons / suggestions, please continue to follow up .. This gave me headaches for several days!
Mardoxx
source share