HTML Modal Popup

html Modal popup

Here's a plain-JavaScript example:

var modal = document.getElementById('modal');var shade = document.getElementById('shade');document.getElementById('start').onclick = function() {  modal.style.display = shade.style.display = 'block';};document.getElementById('close').onclick = function() {  modal.style.display = shade.style.display = 'none';};
// This code is a workaround for IE6's lack of support for the// position: fixed style.//if (!('maxHeight' in document.body.style)) { function modalsize() { var top = document.documentElement.scrollTop; var winsize = document.documentElement.offsetHeight; var docsize = document.documentElement.scrollHeight; shade.style.height = Math.max(winsize, docsize) + 'px'; modal.style.top = top + Math.floor(winsize / 3) + 'px'; }; modal.style.position = shade.style.position = 'absolute'; window.onscroll = window.onresize = modalsize; modalsize();}
body {  margin: 0;}
#shade,#modal { display: none;}
#shade { position: fixed; z-index: 100; top: 0; left: 0; width: 100%; height: 100%;}
#modal { position: fixed; z-index: 101; top: 33%; left: 25%; width: 50%;}
#shade { background: silver; opacity: 0.5; filter: alpha(opacity=50);}
<div id="shade"></div><div id="modal">  <textarea rows="5" cols="25"></textarea>  <button id="close">Close</button></div>
<p> <button id="start">Start</button></p>

Why is my modal popup not showing no matter what i do?

your modal.is-visible class isn't offering a display option and it's not inheriting it from your clicking on the button. If you add display: blockor flex or whatever you desire, that will allow it to actually appear. If you check your original code and then check the inspector, you'll notice that the modal is in fact there, it's just set to display: none

const openEls = document.querySelectorAll("[data-open]");
const closeEls = document.querySelectorAll("[data-close]");
const isVisible = "is-visible";

for (const el of openEls) {
el.addEventListener("click", function() {
const modalId = this.dataset.open;
document.getElementById(modalId).classList.add(isVisible);
});
}

for (const el of closeEls) {
el.addEventListener("click", function() {
this.parentElement.parentElement.parentElement.classList.remove(isVisible);
});
}

document.addEventListener("click", e => {
if (e.target == document.querySelector(".modal.is-visible")) {
document.querySelector(".modal.is-visible").classList.remove(isVisible);
}
});

document.addEventListener("keyup", e => {
// if we press the ESC
if (e.key == "Escape" && document.querySelector(".modal.is-visible")) {
document.querySelector(".modal.is-visible").classList.remove(isVisible);
}
});
.open-modal {
font-weight: bold;
background: var(--blue);
color: var(--white);
padding: 0.75rem 1.75rem;
margin-bottom: 1rem;
border-radius: 5px;
}

/* MODAL
–––––––––––––––––––––––––––––––––––––––––––––––––– */

.modal {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
padding: 1rem;
background: var(--black);
cursor: pointer;
visibility: hidden;
opacity: 0;
transition: all 0.35s ease-in;
}

.modal.is-visible {
display: block;
visibility: visible;
opacity: 1;
overflow: scroll;
}

.modal-dialog {
position: relative;
max-width: 800px;
max-height: 80vh;
border-radius: 5px;
background: var(--white);
overflow: auto;
cursor: default;
}

.modal-dialog>* {
padding: 1rem;
}

.modal-header,
.modal-footer {
background: var(--lightgray);
}

.modal-header {
display: flex;
align-items: center;
justify-content: space-between;
}

.modal-header .close-modal {
font-size: 1.5rem;
}

.modal p+p {
margin-top: 1rem;
}

/* ANIMATIONS
–––––––––––––––––––––––––––––––––––––––––––––––––– */

[data-animation] .modal-dialog {
opacity: 0;
transition: all 0.5s var(--bounceEasing);
}

[data-animation].is-visible .modal-dialog {
opacity: 1;
transition-delay: 0.2s;
}

[data-animation="slideInOutDown"] .modal-dialog {
transform: translateY(100%);
}

[data-animation="slideInOutTop"] .modal-dialog {
transform: translateY(-100%);
}

[data-animation="slideInOutLeft"] .modal-dialog {
transform: translateX(-100%);
}

[data-animation="slideInOutRight"] .modal-dialog {
transform: translateX(100%);
}

[data-animation="zoomInOut"] .modal-dialog {
transform: scale(0.2);
}

[data-animation="rotateInOutDown"] .modal-dialog {
transform-origin: top left;
transform: rotate(-1turn);
}

[data-animation="mixInAnimations"].is-visible .modal-dialog {
animation: mixInAnimations 2s 0.2s linear forwards;
}

[data-animation="slideInOutDown"].is-visible .modal-dialog,
[data-animation="slideInOutTop"].is-visible .modal-dialog,
[data-animation="slideInOutLeft"].is-visible .modal-dialog,
[data-animation="slideInOutRight"].is-visible .modal-dialog,
[data-animation="zoomInOut"].is-visible .modal-dialog,
[data-animation="rotateInOutDown"].is-visible .modal-dialog {
transform: none;
}

@keyframes mixInAnimations {
0% {
transform: translateX(-100%);
}
10% {
transform: translateX(0);
}
20% {
transform: rotate(20deg);
}
30% {
transform: rotate(-20deg);
}
40% {
transform: rotate(15deg);
}
50% {
transform: rotate(-15deg);
}
60% {
transform: rotate(10deg);
}
70% {
transform: rotate(-10deg);
}
80% {
transform: rotate(5deg);
}
90% {
transform: rotate(-5deg);
}
100% {
transform: rotate(0deg);
}
}
<!DOCTYPE html>

<html class="no-js" lang="zxx">

<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title></title>
<meta name="description" content="" />
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="manifest" href="site.webmanifest" />
<link rel="shortcut icon" type="image/x-icon" href="assets/img/favicon.ico" />

<!-- CSS here -->
<link rel="stylesheet" href="assets/css/bootstrap.min.css" />
<link rel="stylesheet" href="assets/css/owl.carousel.min.css" />
<link rel="stylesheet" href="assets/css/flaticon.css" />
<link rel="stylesheet" href="assets/css/stylemodal.css">
<link rel="stylesheet" href="assets/css/ionicons.min.css">
<link rel="stylesheet" href="assets/css/slicknav.css" />
<link rel="stylesheet" href="assets/scss/stylemodal.scss">
<link rel="stylesheet" href="assets/css/animate.min.css" />
<link rel="stylesheet" href="assets/css/magnific-popup.css" />
<link rel="stylesheet" href="assets/css/fontawesome-all.min.css" />
<link rel="stylesheet" href="assets/css/themify-icons.css" />
<link rel="stylesheet" href="assets/css/slick.css" />
<link rel="stylesheet" href="assets/css/nice-select.css" />
<link rel="stylesheet" href="assets/css/style.css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.min.css" rel="stylesheet">
</head>

<body>
<!-- Preloader Start -->
<div id="preloader-active">
<div class="preloader d-flex align-items-center justify-content-center">
<div class="preloader-inner position-relative">
<div class="preloader-circle"></div>
<div class="preloader-img pere-text">
<img src="assets/img/logo/ICON FAVICON 2.png" alt="Sample Image" />
</div>
</div>
</div>
</div>
<!-- Preloader Start -->

<div class="navigation-wrap start-header start-style">
<div class="container">
<div class="row">
<div class="col-12">
<nav class="navbar navbar-expand-md navbar-light">

<a href="index.html"><img src="assets/img/logo/ LOGO (1) 3.png" alt="Sample Image" /></a>

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto py-4 py-md-0">
<li class="nav-item pl-4 pl-md-0 ml-0 ml-md-4 active">
<a style="font-weight: 400; font-family: SF Pro Rounded, sans-serif; font-style: normal; font-size: 16px; color: #AEB0C1;" class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Services</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="what-we-do.html"><img src="assets/img/gallery/pointer.png" alt="Sample Image"> What we do</a>
<a class="dropdown-item" href="dedicated-team.html"><img src="assets/img/gallery/pointer.png" alt="Sample Image"> Dedicated team</a>
<a class="dropdown-item" href="product-engineering.html"><img src="assets/img/gallery/pointer.png" alt="Sample Image"> Product engineering</a>
<a class="dropdown-item" href="development.html"><img src="assets/img/gallery/pointer.png" alt="Sample Image"> Development</a>
</div>
</li>
<li class="nav-item pl-4 pl-md-0 ml-0 ml-md-4">
<a style="font-weight: 400; font-family: SF Pro Rounded, sans-serif; font-style: normal; font-size: 16px; color: #AEB0C1;" class="nav-link" href="about-us.html">About</a>
</li>
<li class="nav-item pl-4 pl-md-0 ml-0 ml-md-4">
<a style="font-weight: 400; font-family: SF Pro Rounded, sans-serif; font-style: normal; font-size: 16px; color: #AEB0C1;" class="nav-link" href="projects.html">Projects</a>
</li>

<li class="nav-item pl-4 pl-md-0 ml-0 ml-md-4">
<a style="font-weight: 400; font-family: SF Pro Rounded, sans-serif; font-style: normal; font-size: 16px; color: #AEB0C1;" class="nav-link" href="careers.html">Career</a>
</li><br>
<li style="padding-left: 20px;"><button style="background: #489DF6;" class="genric-btn success circle">
Get in touch
</button>

</li>

</ul>
</div>

</nav>
</div>
</div>
</div>
</div>

<main style="margin-bottom: 0px;">

<div class="btn-group">
<button type="button" class="open-modal" data-open="modal1">
Launch first modal with a slide animation
</button>
<button type="button" class="open-modal" data-open="modal2">
Launch second modal with a bunch of animations
</button>
</div>

<div class="modal" id="modal1" data-animation="slideInOutLeft">
<div class="modal-dialog">
<header class="modal-header">
The header of the first modal
<button class="close-modal" aria-label="close modal" data-close>

</button>
</header>
<section class="modal-content">
<p><strong>Press ✕, ESC, or click outside of the modal to close it</strong></p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo repellendus reprehenderit accusamus totam ratione! Nesciunt, nemo dolorum recusandae ad ex nam similique dolorem ab perspiciatis qui. Facere, dignissimos. Nemo, ea.</p>
<p>Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which
of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces
no resultant pleasure</p>
<p>Nullam vitae enim vel diam elementum tincidunt a eget metus. Curabitur finibus vestibulum rutrum. Vestibulum semper tellus vitae tortor condimentum porta. Sed id ex arcu. Vestibulum eleifend tortor non purus porta dapibus</p>
<p>Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which
of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces
no resultant pleasure</p>
</section>
<footer class="modal-footer">
The footer of the first modal
</footer>
</div>
</div>

<div class="modal" id="modal2" data-animation="mixInAnimations">
<div class="modal-dialog">
<header class="modal-header">
The header of the second modal
<button class="close-modal" aria-label="close modal" data-close>

</button>
</header>
<section class="modal-content">
<p><strong>Press ✕, ESC, or click outside of the modal to close it</strong></p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo repellendus reprehenderit accusamus totam ratione! Nesciunt, nemo dolorum recusandae ad ex nam similique dolorem ab perspiciatis qui. Facere, dignissimos. Nemo, ea.</p>
<p>Nullam vitae enim vel diam elementum tincidunt a eget metus. Curabitur finibus vestibulum rutrum. Vestibulum semper tellus vitae tortor condimentum porta. Sed id ex arcu. Vestibulum eleifend tortor non purus porta dapibus</p>
</section>
<footer class="modal-footer">
The footer of the second modal
</footer>
</div>
</div>

<footer class="page-footer">
<small>Made with <span>❤</span> by <a href="http://georgemartsoukos.com/" target="_blank">George Martsoukos</a>
</small>
</footer>

Add Focus To Pop Up / Modal On Click For Tabbing / Accessibility - JavaScript

You have to add focus to the pop-up right after this appears, when you do it simultaneously with closeButton.focus() only it won't work that's why I'm using setTimeout(() => closeButton.focus(), 1), this will added it focus after a 1 millisecond.

At first, focus on a button isn't visible, it become visible when arrow keys are pressed, so I make it visible styling it:

      .close:focus {
border: 2px solid black;
border-radius: 5px;
}

The whole code:

      let clickMe = document.querySelector("#click-me"),
modal = document.querySelector(".modal"),
closeButton = document.querySelector(".close");

clickMe.addEventListener("click", () => {
setTimeout(() => closeButton.focus(), 1);
modal.style.display = "flex";
});

closeButton.addEventListener("click", () => {
modal.style.display = "none";
});
      body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}

input,
button {
margin: 1rem;
padding: 0.5rem;
}
.click-me {
display: block;
}

.modal {
display: none;
flex-direction: column;
width: 100%;
height: 100%;
justify-content: center;
align-items: center;
background: gray;
position: absolute;
}

form {
display: flex;
}
.close:focus {
border: 2px solid black;
border-radius: 5px;
}
    <button id="click-me">Click Me</button>
<form action="">
<input type="text" placeholder="An Input" />
<input type="text" placeholder="An Input" />
<input type="text" placeholder="An Input" />
<input type="text" placeholder="An Input" />
</form>

<div class="modal">
<button class="close">Close x</button>
<button>More Buttons</button>
<button>More Buttons</button>
</div>


Related Topics



Leave a reply



Submit