Created
September 29, 2020 14:50
-
-
Save victorpavlov/e44266b1e481c21740ca2f5e662ffad9 to your computer and use it in GitHub Desktop.
jQuery on hover delay realisation: http://jsfiddle.net/AndreasPizsa/NzvKC/
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 ($, window, delay) { | |
// http://jsfiddle.net/AndreasPizsa/NzvKC/ | |
var theTimer = 0; | |
var theElement = null; | |
var theLastPosition = {x:0,y:0}; | |
$('[data-toggle]') | |
.closest('li') | |
.on('mouseenter', function (inEvent) { | |
if (theElement) theElement.removeClass('open'); | |
window.clearTimeout(theTimer); | |
theElement = $(this); | |
theTimer = window.setTimeout(function () { | |
theElement.addClass('open'); | |
}, delay); | |
}) | |
.on('mousemove', function (inEvent) { | |
if(Math.abs(theLastPosition.x - inEvent.ScreenX) > 4 || | |
Math.abs(theLastPosition.y - inEvent.ScreenY) > 4) | |
{ | |
theLastPosition.x = inEvent.ScreenX; | |
theLastPosition.y = inEvent.ScreenY; | |
return; | |
} | |
if (theElement.hasClass('open')) return; | |
window.clearTimeout(theTimer); | |
theTimer = window.setTimeout(function () { | |
theElement.addClass('open'); | |
}, delay); | |
}) | |
.on('mouseleave', function (inEvent) { | |
window.clearTimeout(theTimer); | |
theElement = $(this); | |
theTimer = window.setTimeout(function () { | |
theElement.removeClass('open'); | |
}, delay); | |
}); | |
})(jQuery, window, 200); // 200 is the delay in milliseconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment