JSFiddle - React, Tailwind, and code Playground
by TheCyberNash
HTML
<p id="trigger">Click here</p>
<div id="content">
</div>
CSS
.scotch-overlay {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
opacity: 0;
width: 100%;
height: 100%;
-webkit-transition: 1ms opacity ease;
-moz-transition: 1ms opacity ease;
-ms-transition: 1ms opacity ease;
-o-transition: 1ms opacity ease;
transition: 1ms opacity ease;
background: rgba(0, 0, 0, .5);
}
.scotch-modal {
position: absolute;
z-index: 9999;
top: 50%;
left: 50%;
opacity: 0;
width: 94%;
padding: 24px 20px;
-webkit-transition: 1ms opacity ease;
-moz-transition: 1ms opacity ease;
-ms-transition: 1ms opacity ease;
-o-transition: 1ms opacity ease;
transition: 1ms opacity ease;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
border-radius: 2px;
background: #1d1d1d;
}
.scotch-modal.scotch-open.scotch-anchored {
top: 20px;
-webkit-transform: translate(-50%, 0);
-moz-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
-o-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
.scotch-modal.scotch-open {
opacity: 1;
}
.scotch-overlay.scotch-open {
opacity: 1;
}
/* Close Button */
.scotch-close {
font-family: Helvetica, Arial, sans-serif;
font-size: 24px;
line-height: 12px;
position: absolute;
top: 5px;
right: 5px;
padding: 5px 5px 5px;
cursor: pointer;
color: #1d1d1d;
border: 0;
outline: none;
background: grey;
border-radius: 50%;
}
.scotch-close:hover {
background: #4b4b4b;
}
/* Default Animation */
.scotch-overlay.fade-and-drop {
display: block;
opacity: 0;
}
.scotch-modal.fade-and-drop {
top: -300%;
opacity: 1;
display: block;
}
.scotch-modal.fade-and-drop.scotch-open {
top: 50%;
-webkit-transition: 500ms top 500ms ease;
-moz-transition: 500ms top 500ms ease;
-ms-transition: 500ms top 500ms ease;
-o-transition: 500ms top 500ms ease;
transition: 500ms...
JavaScript
// Create an immediately invoked functional expression to wrap our code
(function() {
// Define our constructor
this.Modal = function() {
// Create global element references
this.closeButton = null;
this.modal = null;
this.overlay = null;
// Determine proper prefix
this.transitionEnd = transitionSelect();
// Define option defaults
var defaults = {
autoOpen: false,
className: 'fade-and-drop',
closeButton: true,
content: "",
maxWidth: 600,
minWidth: 280,
overlay: true
}
// Create options by extending defaults with the passed in arugments
if (arguments[0] && typeof arguments[0] === "object") {
this.options = extendDefaults(defaults, arguments[0]);
}
if (this.options.autoOpen === true) this.open();
}
// Public Methods
Modal.prototype.close = function() {
var _ = this;
this.modal.className = this.modal.className.replace(" scotch-open", "");
this.overlay.className = this.overlay.className.replace(" scotch-open",
"");
this.modal.addEventListener(this.transitionEnd, function() {
_.modal.parentNode.removeChild(_.modal);
});
this.overlay.addEventListener(this.transitionEnd, function() {
if (_.overlay.parentNode) _.overlay.parentNode.removeChild(_.overlay);
});
}
Modal.prototype.open = function() {
buildOut.call(this);
initializeEvents.call(this);
window.getComputedStyle(this.modal).height;
this.modal.className = this.modal.className +
(this.modal.offsetHeight > window.innerHeight ?
" scotch-open scotch-anchored" : " scotch-open");
this.overlay.className = this.overlay.className + " scotch-open";
}
// Private Methods
function buildOut() {
var content, contentHolder, docFrag;
/*
* If content is an HTML string, append the HTML string.
* If...