the simplest modal you ever did see. – Code on GitHub »
rel="modal:open"
and set the href
attribute to the modal's DOM id.<!DOCTYPE html>
<html>
<head>
</style>
<!-- Don't forget to include jQuery ;) -->
<script src="jquery.modal.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!-- Modal HTML embedded directly into document -->
<div id="ex1" style="display:none;">
<p>Thanks for clicking. That felt good. <a href="#" rel="modal:close">Close</a> or press ESC</p>
</div>
<!-- Link to open the modal -->
<p><a href="#ex1" rel="modal:open">Open Modal</a></p>
</body>
</html>
Demo: Open Modal
This example demonstrates how visually customizable the modal is.
This example shows how modals are centered automatically. It also demonstrates how a vertical scrollbar appears whenever the modal content overflows.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
This first example uses rel="modal:open"
to automatically load the page contents into a modal via AJAX:
<a href="ajax.html" rel="modal:open">example</a>
This second example shows how you can manually load AJAX pages into a modal. Note that the AJAX response must be wrapped in a div with class modal
:
<!-- Normal link -->
<a href="ajax.html" id="manual-ajax">second example</a>
// Open modal in AJAX callback
$('#manual-ajax').click(function(event) {
event.preventDefault();
$.get(this.href, function(html) {
$(html).appendTo('body').modal();
});
});
<!-- AJAX response must be wrapped in the modal's root class. -->
<div class="modal">
<p>Second AJAX Example!</p>
</div>
If you want more spinner styles, check out ajaxload.info.
This example demonstrates how to disable the default methods of closing the modal:
$("#sticky").modal({
escapeClose: false,
clickClose: false,
showClose: false
});
If you do this, be sure to provide the user with an alternate method of closing the window.
This example shows how only one modal can be open at a time. If you open a new modal while an existing modal is open, the existing modal is closed first.
If you need to stack multiple modals at the same time, just set the closeExisting
option to false
.
See an example here.
$('#sub-modal').modal({
closeExisting: false
});
This example shows how you can do a simple fade by specifying the fadeDuration
option.
$("#fade").modal({
fadeDuration: 100
});
You can also use fadeDelay
to control the point during the overlay's fade in at which the modal fades in. For example, to fade in the modal when the overlay transition is 50% complete:
$("#fade").modal({
fadeDuration: 1000,
fadeDelay: 0.50
});
The default value is 1.0
, meaning the window transition begins once the overlay transition has finished. Values greater than 1.0
mean there is a delay between the completed overlay transition and the start of the window transition, example:
$("#fade").modal({
fadeDuration: 1000,
fadeDelay: 1.75 // Will fade in 750ms after the overlay finishes.
});
Tip: set fadeDelay: 0
to have the overlay and window fade in simultaneously.
In the spirit of keeping this library small, fading is the only supported transition. When the modal is closed, both the overal and window transition out simultaneously.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
This modal starts fading in once the overlay is 50% faded in.
This modal starts fading in well after the overlay has finished transitioning.
This example demonstrates how to add extra classes to the close button (for custom styles for the close button):
$("#custom-close").modal({
closeClass: 'icon-remove',
closeText: '!'
});
And then of course your custom CSS
.modal a.close-modal[class*="icon-"] {
top: -10px;
right: -10px;
width: 20px;
height: 20px;
color: #fff;
line-height: 1.25;
text-align: center;
text-decoration: none;
text-indent: 0;
background: #900;
border: 2px solid #fff;
-webkit-border-radius: 26px;
-moz-border-radius: 26px;
-o-border-radius: 26px;
-ms-border-radius: 26px;
-moz-box-shadow: 1px 1px 5px rgba(0,0,0,0.5);
-webkit-box-shadow: 1px 1px 5px rgba(0,0,0,0.5);
box-shadow: 1px 1px 5px rgba(0,0,0,0.5);
}
This modal has a fancy-shmancy close button.