Skip to content

Instantly share code, notes, and snippets.

@victorpavlov
Created September 29, 2020 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorpavlov/e44266b1e481c21740ca2f5e662ffad9 to your computer and use it in GitHub Desktop.
Save victorpavlov/e44266b1e481c21740ca2f5e662ffad9 to your computer and use it in GitHub Desktop.
jQuery on hover delay realisation: http://jsfiddle.net/AndreasPizsa/NzvKC/
(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