Skip to content

Instantly share code, notes, and snippets.

@MrVladevoit
Created June 15, 2017 06:51
Show Gist options
  • Save MrVladevoit/6acbc8636dcb1ff64f5cd34614d1e15c to your computer and use it in GitHub Desktop.
Save MrVladevoit/6acbc8636dcb1ff64f5cd34614d1e15c to your computer and use it in GitHub Desktop.
gp popup
(function ($) {
/*----------------------------------------
POPUPS / MODALS
----------------------------------------*/
var popup = $(".popup"),
popupOpen = $("[data-popup='open']"),
popupClose = $("[data-popup='close']"),
popupOverlay = $(".popup-overlay");
/* POPUP METHODS */
var methods = {
show : function( options ) {
return this.each(function(){
$(this).addClass('popup_open');
$('body').addClass('popup_lock')
});
},
hide : function( ) {
return this.each(function(){
if (popup.hasClass('popup_open')) {
$(this).removeClass('popup_open');
$('body').removeClass('popup_lock')
$('.popup.popup_open').addClass('popup_close');
setTimeout(function() {
$('.popup').removeClass('popup_close');
}, 400);
}
})
}
};
/* POPUP FUNCTIONS */
$.fn.gpPopup = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.show.apply( this, arguments );
} else {
$.error( 'Метод с именем ' + method + ' не существует' );
}
};
/* CLICK FOR OPEN */
popupOpen.on('click', function(link) {
link.preventDefault();
var popupId = $(this).attr('data-popup-id');
$(popupId).gpPopup();
});
/* CLICK FOR CLOSE */
popupClose.on('click', function(link) {
link.preventDefault();
popup.gpPopup('hide');
});
popupOverlay.on('click', function(link) {
link.preventDefault();
popup.gpPopup('hide');
});
$(this).keydown(function(eventObject){
if (eventObject.which == 27)
popup.gpPopup('hide');
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment