Skip to content

Instantly share code, notes, and snippets.

@mpezzi
Created August 25, 2011 17:12
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 mpezzi/1171181 to your computer and use it in GitHub Desktop.
Save mpezzi/1171181 to your computer and use it in GitHub Desktop.
jQuery Delayed Hide Plugin
/**
* jQuery Delayed Hide Plugin by M. Pezzi
* Version: 1.0 (08/25/11)
* https://gist.github.com/1171181
* Dual licensed under the MIT and GPL licences:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Requires: jQuery v1.4.2 or later
*/
(function($){
$.fn.delayedHide = function(options) {
return this.each(function(){
var self = $(this), o = $.extend({}, $.fn.delayedHide.defaults, options), delay,
$close = $('<div class="close">Close</div>').bind('click', { message: self }, close);
// Append close markup, start delay hide.
o.close && self.append($close);
o.delay && delayStart();
// If a user hovers, cancel the delay.
self.bind('mouseover', function(){
clearTimeout(delay);
});
function delayStart() {
delay = setTimeout(function(){
close();
}, o.delay);
}
function delayStop() {
clearTimeout(delay);
}
function hide() {
self.slideUp(o.animateDuration, o.animateCallback);
}
function close(e) {
delayStop();
hide();
}
});
};
$.fn.delayedHide.defaults = {
delay: 5000,
close: true,
animateDuration: 500,
animateCallback: function() { }
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment