Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created July 5, 2011 19:45
Show Gist options
  • Select an option

  • Save madrobby/1065712 to your computer and use it in GitHub Desktop.

Select an option

Save madrobby/1065712 to your computer and use it in GitHub Desktop.
Little Zepto "delayed hover" plugin, using this with backbone.js events
// (c) 2011 Thomas Fuchs
(function($){
$.fn.delayedHover = function(){
var timeout = null, hovering = false, element = this;
function enter(){
element.trigger('delayed_hover:enter');
timeout = null;
hovering = true;
}
function leave(){
element.trigger('delayed_hover:leave');
timeout = null;
hovering = false;
}
function reset(){
if(timeout) clearTimeout(timeout);
timeout = null;
}
element.bind('mouseover', function(){
if(hovering)
reset();
else
if(!timeout) timeout = setTimeout(enter, 250);
});
element.bind('mouseout', function(){
if(hovering){
if(timeout) reset();
timeout = setTimeout(leave, 150);
} else {
reset();
}
});
}
})(Zepto);
@madrobby
Copy link
Copy Markdown
Author

madrobby commented Jul 5, 2011

Note that this is just a quick hack. I welcome any suggestions for improvement.

@aamirafridi
Copy link
Copy Markdown

Pass the delay time as an option to the plugin instead of hardcoding it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment