Created
July 5, 2011 19:45
-
-
Save madrobby/1065712 to your computer and use it in GitHub Desktop.
Little Zepto "delayed hover" plugin, using this with backbone.js events
This file contains hidden or 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
// (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); |
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
Note that this is just a quick hack. I welcome any suggestions for improvement.