Created
June 20, 2014 09:05
-
-
Save kyaido/dec4c6376d80ae348a14 to your computer and use it in GitHub Desktop.
jquery.peekaboo.js
This file contains 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
(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