Skip to content

Instantly share code, notes, and snippets.

@ursuleacv
Last active June 13, 2016 13:39
Show Gist options
  • Save ursuleacv/cdc186ad3a392b48de9857b412d3bd0f to your computer and use it in GitHub Desktop.
Save ursuleacv/cdc186ad3a392b48de9857b412d3bd0f to your computer and use it in GitHub Desktop.
A very simple modal dialog
DEMO: https://jsfiddle.net/k3zfzxgx/
<!-- HTML -->
<div id="loginModal" class="modal">
<div class="modal-content modal-login">
<div class="modal-header">
<span class="modal-close">×</span>
<h2>My title</h2>
</div>
<div class="modal-body">
<p>My modal dialog</p>
</div>
<div class="modal-footer">
Modal footer
</div>
</div>
</div>
function ModalDialog(options) {
var self = this;
this.modalEl = options.el;
// Get the modal
this.modal = document.getElementById(this.modalEl);
this.close = document.getElementsByClassName("modal-close")[0];
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == self.modal) {
self.modal.style.display = "none";
}
};
this.close.onclick = function() {
self.modal.style.display = "none";
};
}
ModalDialog.prototype.open = function() {
this.modal.style.display = "block";
};
$(function() {
var loginDialog = new ModalDialog({
'el': 'loginModal'
});
$('.open-modal-element').on('click', function(){
loginDialog.open();
});
});
.modal .modal-login {
max-width: 400px;
}
.modal-header h2{
margin: 0;
}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 0px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s;
border-radius: 4px;
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.modal-close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.modal-close:hover,
.modal-close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #313334;
color: white;
}
.modal-body {padding: 2px 16px; color: #9fa1a1}
.modal-footer {
padding: 2px 16px;
background-color: #ccc;
color: white;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment