Simply add a click
event listener to the close button that triggers the togglePopup
function:
function togglePopup() {
document.getElementById("popup-1").classList.toggle("active");
}
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap');
.popup .overlay {
position: fixed;
top: 0px;
left: 0px;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.7);
z-index: 1;
display: none;
}
.popup .content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
background: #fff;
width: 450px;
height: 220px;
z-index: 2;
text-align: center;
padding: 20px;
box-sizing: border-box;
}
.popup .close-btn {
cursor: pointer;
position: absolute;
right: 20px;
top: 20px;
width: 30px;
height: 30px;
background: #222;
color: #fff;
font-size: 25px;
font-weight: 600;
line-height: 30px;
text-align: center;
border-radius: 50%;
}
.popup.active .overlay {
display: block;
}
.popup.active .content {
transition: all 300ms ease-in-out;
transform: translate(-50%, -50%) scale(1);
}
h1 {
font-family: 'Source Sans Pro', sans-serif;
}
p {
font-family: 'Source Sans Pro', sans-serif;
}
button {
font-family: 'Source Sans Pro', sans-serif;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="script.js"></script>
</body>
</html>
<div class="popup" id="popup-1">
<div class="overlay"></div>
<div class="content">
<div class="close-btn" onclick="togglePopup()">×</div>
<h1>Test Popup!</h1>
<p>Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup!</p>
</div>
</div>
<button onclick="togglePopup()">Open Popup</button>
If you want the popup to show up after 10 seconds, utilize the setTimeout
function as shown below:
function togglePopup() {
document.getElementById("popup-1").classList.toggle("active");
}
setTimeout(togglePopup, 2000); //change the second parameter to 10000 for 10 seconds delay.
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap');
.popup .overlay {
position: fixed;
top: 0px;
left: 0px;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.7);
z-index: 1;
display: none;
}
.popup .content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
background: #fff;
width: 450px;
height: 220px;
z-index: 2;
text-align: center;
padding: 20px;
box-sizing: border-box;
}
.popup .close-btn {
cursor: pointer;
position: absolute;
right: 20px;
top: 20px;
width: 30px;
height: 30px;
background: #222;
color: #fff;
font-size: 25px;
font-weight: 600;
line-height: 30px;
text-align: center;
border-radius: 50%;
}
.popup.active .overlay {
display: block;
}
.popup.active .content {
transition: all 300ms ease-in-out;
transform: translate(-50%, -50%) scale(1);
}
h1 {
font-family: 'Source Sans Pro', sans-serif;
}
p {
font-family: 'Source Sans Pro', sans-serif;
}
button {
font-family: 'Source Sans Pro', sans-serif;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="script.js"></script>
</body>
</html>
<div class="popup" id="popup-1">
<div class="overlay"></div>
<div class="content">
<div class="close-btn" onclick="togglePopup()">×</div>
<h1>Test Popup!</h1>
<p>Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup! Test Popup!</p>
</div>
</div>
<button onclick="togglePopup()">Open Popup</button>