So, you want to create your own modal block using jQuery instead of using an existing plugin? ... OK, let it play (as already mentioned, using pop-ups is not a user-friendly solution):
Your checklist:
- the trigger - the shadow layer - the modal box size and position - add content to modal and display it along the shadow
1) A trigger is a simple html link to open content inside a modal
<a href="http://jsfiddle.net" class="myModal" data-width="400" data-height="300">open url</a>
... we will pass the size of the modality through the data-width and data-height attributes (HTML5).
2) The shadow level is the html structure that we will add to the body after the trigger. We can set the structure in js variable
var shadow = "<div class='shadow'></div>";
3) As we already mentioned, the size of the modality is set via some data-* attributes in the link. We will need to do the math
var modalWidth = $(this).data("width"); var modalHeight = $(this).data("height"); var modalX = (($(window).innerWidth()) - modalWidth) / 2;
NOTE : $(this) is our .myModal trigger .myModal , which we will later enter the .on("click") method. BTW, .on() method requires jQuery v1.7 +
4) Now we need to create a modal html structure and pass the contents of href . We will create a function
function modal(url) { return '<div id="modal"><a id="closeModal" title="close" href="javascript:;"><img src="http://findicons.com/files/icons/2212/carpelinx/64/fileclose.png" alt="close" /></a><iframe src="' + url + '"></iframe></div>'; }
... as you can see, our structure contains a close button to remove the modal and shadow layer. The function also receives a parameter when called ( url ), which allows you to set the src attribute of the iframe tag.
NOTE : we must use the iframe tag to open external URLs, however we must always consider the same origin of the policy and other security restrictions when using iframes.
So now we have to collect all the events after we click on our .myModal trigger, which adds a shadow and a modal field to the body and removes them when we click the close button, therefore
$(".myModal").on("click", function(e) { e.preventDefault(); // get size and position modalWidth = $(this).data("width"); modalHeight = $(this).data("height"); modalX = (($(window).innerWidth()) - modalWidth) / 2; modalY = (($(window).innerHeight()) - modalHeight) / 2; // append shadow layer $(shadow).prependTo("body").css({ "opacity": 0.7 }); // append modal (call modal() and pass url) $(modal(this.href)).appendTo("body").css({ "top": modalY, "left": modalX, "width": modalWidth, "height": modalHeight }); // close and remove $("#closeModal").on("click", function() { $("#modal, .shadow").remove(); }); }); // on
STYLE : of course, we will need some basic CSS style for our modal elements to work correctly:
.shadow {width: 100%; height: 100%; position: fixed; background-color: #444; top: 0; left:0; z-index: 400} #modal {z-index: 500; position: absolute; background: #fff; top: 50px;} #modal iframe {width: 100%; height: 100%} #closeModal {position: absolute; top: -15px; right: -15px; font-size: 0.8em; } #closeModal img {width: 30px; height: 30px;}
* WATCH DEMO *
BONUS : you can also bind a keyup event to close the modal key with the escape key
$(document).keyup(function(event) { if (event.keyCode === 27) { $("#modal, .shadow").remove(); } });
LAST NOTE : The code is subject to many improvements and optimizations, but is the main location of many lightboxes. My last recommendation: use fancybox for more advanced functionality ... sometimes you should not try to reinvent the wheel; )