Skip to content

Instantly share code, notes, and snippets.

@kyaido
Created June 20, 2014 09:05
Show Gist options
  • Save kyaido/dec4c6376d80ae348a14 to your computer and use it in GitHub Desktop.
Save kyaido/dec4c6376d80ae348a14 to your computer and use it in GitHub Desktop.
jquery.peekaboo.js
(function($) {
$.fn.peekaboo = function(options) {
if (this.length === 0) { return false; }
// user option
var conf = $.extend({
duration: 400,
activeClass: 'active'
}, options);
this.each(function() {
var self = $(this);
var target = self.find('.js-peekabooTarget');
var trigger = self.find('.js-peekabooTrigger');
var closeTrigger = self.find('.js-peekabooCloseTrigger');
var close = function(e) {
e.preventDefault();
self.removeClass(conf.activeClass);
target.stop().fadeOut(conf.duration);
};
// init
target.hide();
// event bind
trigger.on('click', function(e) {
e.preventDefault();
self.toggleClass(conf.activeClass);
target.stop().fadeToggle(conf.duration);
});
closeTrigger.on('click', function(e) {
close(e);
});
$(document).on('click', function(e) {
if ( !$.contains(self[0], e.target) && self.hasClass(conf.activeClass) ) {
close(e);
}
});
});
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment