From 504ae07545588d850cd6622d14f17e35ca70e00e Mon Sep 17 00:00:00 2001 From: arthurhamon Date: Thu, 12 Feb 2015 17:02:20 +0100 Subject: [PATCH] Adding new options, href and closeCallback The href option allows you to lunch a modal from another element. href require an ID or a CLASS. The closeCallback option is fire when you close the modal. --- jquery.leanModal.js | 132 ++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 71 deletions(-) diff --git a/jquery.leanModal.js b/jquery.leanModal.js index facf07e..cf03512 100644 --- a/jquery.leanModal.js +++ b/jquery.leanModal.js @@ -1,73 +1,63 @@ (function($){ - - $.fn.extend({ - - leanModal: function(options) { - - var defaults = { - top: 100, - overlay: 0.5, - closeButton: null - } - - var overlay = $("
"); - - $("body").append(overlay); - - options = $.extend(defaults, options); - - return this.each(function() { - - var o = options; - - $(this).click(function(e) { - - var modal_id = $(this).attr("href"); - - $("#lean_overlay").click(function() { - close_modal(modal_id); - }); - - $(o.closeButton).click(function() { - close_modal(modal_id); - }); - - var modal_height = $(modal_id).outerHeight(); - var modal_width = $(modal_id).outerWidth(); - - $('#lean_overlay').css({ 'display' : 'block', opacity : 0 }); - - $('#lean_overlay').fadeTo(200,o.overlay); - - $(modal_id).css({ - - 'display' : 'block', - 'position' : 'fixed', - 'opacity' : 0, - 'z-index': 11000, - 'left' : 50 + '%', - 'margin-left' : -(modal_width/2) + "px", - 'top' : o.top + "px" - - }); - - $(modal_id).fadeTo(200,1); - - e.preventDefault(); - - }); - - }); - - function close_modal(modal_id){ - - $("#lean_overlay").fadeOut(200); - - $(modal_id).css({ 'display' : 'none' }); + $.fn.extend({ + leanModal:function(options){ + var defaults = { + top:50, + overlay:0.5, + closeButton:null, + href:null, + closeCallback: function(){} + }; - } - - } - }); - -})(jQuery); \ No newline at end of file + var overlay = $("
"); + $("body").append(overlay); + + options = $.extend(defaults,options); + + return this.each(function(){ + var o=options; + $(this).click(function(e){ + if (o.href == null){ + var modal_id=$(this).attr("href"); + }else{ + var modal_id=o.href; + } + $("#lean_overlay").click(function(){ + close_modal(modal_id) + }); + $(o.closeButton).click(function(){ + close_modal(modal_id) + }); + var modal_height=$(modal_id).outerHeight(); + var modal_width=$(modal_id).outerWidth(); + + $("#lean_overlay").css({ + "display":"block", + opacity:0 + }); + $("#lean_overlay").fadeTo(200,o.overlay); + $(modal_id).css({ + "display":"block", + "position":"fixed", + "opacity":0, + "z-index":11000, + "left":50+"%", + "margin-left":-(modal_width/2)+"px", + "top":o.top+"%", + "margin-top":-(modal_height/2)+"px" + }); + $(modal_id).fadeTo(200,1); + $('html, body').animate({ scrollTop: 0 }, 200); + e.preventDefault() + }) + }); + + function close_modal(modal_id){ + $("#lean_overlay").fadeOut(200); + $(modal_id).css({ + "display":"none" + }); + defaults.closeCallback.call(); + } + } +})})(jQuery);