Project#2 : Modal Window
1) prettierrc file
{
"singleQuote": true,
"arrowParens": "avoid"
}
2) index.html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="style.css" />
<title>Modal window</title>
</head>
<body>
<button class="show-modal">Show modal 1</button>
<button class="show-modal">Show modal 2</button>
<button class="show-modal">Show modal 3</button>
<div class="modal hidden">
<button class="close-modal">×</button>
<h1>I'm a modal window 😍</h1>
<p>
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.
</p>
</div>
<div class="overlay hidden"></div>
<script src="script.js"></script>
</body>
</html>
3) script.js file
'use strict';
const modal = document.querySelector('.modal');
// This is the modal window which will open when we click the button.
const overlay = document.querySelector('.overlay');
// This the dark background overlay which is in the background when we open the modal.
const btnCloseModal = document.querySelector('.close-modal');
// This is the button to close the modal
const btnsOpenModal = document.querySelectorAll('.show-modal');
// we used .querySelectorAll because we have 3 buttons with the same class name =
'.show-modal' and .querySelector will only select the first element with this class name.
// we have 3 buttons to open the modal window and these are the 3 buttons.
const OpenModal = function () {
modal.classList.remove('hidden');
// Here we are removing the hidden class form the modal so that when the respective buttons
are clicked the modal shows.
overlay.classList.remove('hidden');
// We are removing the hidden class from the overlay, so that a nice dark blurry backgorund
color shows when we click the buttons.
};
const closeModal = function () {
modal.classList.add('hidden');
overlay.classList.add('hidden');
};
for (let i = 0; i < btnsOpenModal.length; i++)
btnsOpenModal[i].addEventListener('click', OpenModal);
btnCloseModal.addEventListener('click', closeModal);
overlay.addEventListener('click', closeModal);
// Handling key Press Events.
document.addEventListener('keydown', function (e) {
console.log(e.key);
if (e.key === 'Escape' && !modal.classList.contains('hidden')) {
// this reads as if the pressed key is 'Escape' & if the modal does not contain the
'hidden' class then close the modal
closeModal();
}
});
4) style.css file
* {
margin: 0;
padding: 0;
box-sizing: inherit;
}
html {
font-size: 62.5%;
box-sizing: border-box;
}
body {
font-family: sans-serif;
color: #333;
line-height: 1.5;
height: 100vh;
position: relative;
display: flex;
align-items: flex-start;
justify-content: center;
background: linear-gradient(to top left, #28b487, #7dd56f);
}
.show-modal {
font-size: 2rem;
font-weight: 600;
padding: 1.75rem 3.5rem;
margin: 5rem 2rem;
border: none;
background-color: #fff;
color: #444;
border-radius: 10rem;
cursor: pointer;
}
.close-modal {
position: absolute;
top: 1.2rem;
right: 2rem;
font-size: 5rem;
color: #333;
cursor: pointer;
border: none;
background: none;
}
h1 {
font-size: 2.5rem;
margin-bottom: 2rem;
}
p {
font-size: 1.8rem;
}
/* -------------------------- */
/* CLASSES TO MAKE MODAL WORK */
.hidden {
display: none;
}
.modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 70%;
background-color: white;
padding: 6rem;
border-radius: 5px;
box-shadow: 0 3rem 5rem rgba(0, 0, 0, 0.3);
z-index: 10;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
backdrop-filter: blur(3px);
z-index: 5;
}
Comments
Post a Comment