Skip to content

Instantly share code, notes, and snippets.

@jumanji27
Created July 4, 2013 15:14
Show Gist options
  • Save jumanji27/5928549 to your computer and use it in GitHub Desktop.
Save jumanji27/5928549 to your computer and use it in GitHub Desktop.
// Nikita Lebedev's blog, nazz.me/simple-jquery-popup
(function($) {
$.fn.simplePopup = function() {
var simplePopup = {
// Events
initialize: function(self) {
var popup = $(".js__popup");
var body = $(".js__p_body");
var close = $(".js__p_close");
var string = self[0].className;
var name = string.replace("js__p_", "");
// We redefine the variables if there is an additional popap
if ( !(name === "start") ) {
name = name.replace("_start", "_popup");
popup = $(".js__" + name);
name = name.replace("_", "-");
};
// Call when have click
self.on("click", function() {
simplePopup.show(popup, body);
return false;
});
// Close
body.on("click", function() {
simplePopup.hide(popup, body);
});
close.on("click", function() {
simplePopup.hide(popup, body);
return false;
});
// Closure of the button "esc"
$(window).keyup(function(e) {
if (e.keyCode === 27) {
simplePopup.hide(popup, body);
}
});
},
// Centering method
centering: function(self) {
var marginLeft = -self.width()/2;
return self.css("margin-left", marginLeft);
},
// The overall function of the show
show: function(popup, body) {
simplePopup.centering(popup);
body.removeClass("js__fadeout");
popup.removeClass("js__slide_top");
},
// The overall function of the hide
hide: function(popup, body) {
popup.addClass("js__slide_top");
body.addClass("js__fadeout");
},
};
// In loop looking for what is called
return this.each(function() {
var self = $(this);
simplePopup.initialize(self);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment