I am trying to add a CSS transition to my modal. It should work, except it isn't. The transition is supposed to finish in 200ms, but it's not happening.
I have attached my code snippet below. I've tried many different things, but the result is working. Except when I try to enable the transition using code inspector, it works as expected. I don't know where the issue is.
const bookForm = document.querySelector('.modal-form')
const submitBtn = document.querySelector('.modal-form-btn')
const addNewBookBtn = document.querySelector('.new-book-btn')
const modal = document.querySelector('.bg-modal')
const modalContent = document.querySelector('.modal-content')
function displayAddNewBookModal() {
modal.style.display = 'flex'
modalContent.classList.add('active')
// modal.classList.add('active')
}
function closeAddNewBookModal() {
modal.style.display = 'none'
modalContent.classList.remove('active')
}
addNewBookBtn.onclick = displayAddNewBookModal
modal.onclick = closeAddNewBookModal
@import url('https://fonts.googleapis.com/css2?family=Bebas Neue&display=swap');
* {
/* border: 0; */
padding: 0;
margin: 0;
box-sizing: border-box;
color: #474B44;
/* background: #FEFDEB; */
}
body {
font-family: 'Bebas Neue', cursive;
color: #474B44;
letter-spacing: 1px;
background-color: #FEFDEC;
/* background-color: crimson; */
}
.nav {
padding: 30px;
background-color: #FDFBD8;
border-bottom: 1px solid #5a53530e;
display: flex;
justify-content: space-between;
align-items: center;
}
#login-btn {
padding: 10px 30px;
font-size: 25px;
background-color: rgb(127, 255, 148);
border: 2px solid #474B44;
border-radius: 5px;
}
#library {
font-size: 64px;
}
.new-book-btn {
font-size: 30px;
color: white;
background-color: #EEB868;
border: 2px solid #5a53530e;
border-radius: 5px;
padding: 10px 30px;
}
.bg-modal {
height: 100%;
width: 100%;
position: fixed;
background-color: rgba(0, 0, 0, 0.7);
justify-content: center;
align-items: center;
display: none;
}
.modal-content {
height: 600px;
width: 400px;
background-color: aliceblue;
padding: 40px;
border-radius: 5px;
transform: scale(0);
transition: 200ms ease-in-out;
}
.modal-content.active {
transform: scale(1);
}
.modal-form {
/* margin-top: 10px; */
display: flex;
flex-direction: column;
align-items: center;
gap: 30px;
border-radius: 5px;
justify-content: space-evenly;
}
.modal-dialouge {
font-size: 55px;
}
.modal-input {
width: 90%;
padding: 20px;
font-weight: bold;
font-size: 30px;
}
.modal-form-btn {
padding: 5px 10px;
background-color: yellow;
font-size: 30px;
border-radius: 5px;
font-family: 'Bebas Neue', cursive;
color: #474B44;
border: 2px solid #474B44;
}
.isRead {
display: flex;
gap: 20px;
font-size: 30px;
align-items: center;
}
.modal-checkbox {
width: 25px;
height: 25px;
}
<div >
<div >
<form action="">
<h2 >Add a new book</h2>
<input id="book" type="text" placeholder="Title">
<input id="pages" type="text" placeholder="Pages">
<input id="author" type="text" placeholder="Author">
<div >
<label for="readingStatus">Have you read it?</label>
<input type="checkbox" name="readingStatus" id="readingStatus">
</div>
<button type="button">New Book</button>
</form>
</div>
</div>
<header>
<div >
<h1 id="library">Ishaan's Library</h1>
<button id="login-btn">Login</button>
</div>
<div >
<button >Add Book</button>
</div>
</header>
CodePudding user response:
When the clicked we'll take that event as e
and see where the target
is. To the target is the surrounding area, not the popup, we'll check the target if it has the class bg-modal
const bookForm = document.querySelector('.modal-form')
const submitBtn = document.querySelector('.modal-form-btn')
const addNewBookBtn = document.querySelector('.new-book-btn')
const modal = document.querySelector('.bg-modal')
const modalContent = document.querySelector('.modal-content')
function displayAddNewBookModal() {
modal.classList.add('active')
}
function closeAddNewBookModal() {
modal.classList.remove('active')
}
addNewBookBtn.onclick = displayAddNewBookModal
modal.onclick = (e) => {
if ([...e.target.classList].includes('bg-modal')) closeAddNewBookModal();
}
@import url('https://fonts.googleapis.com/css2?family=Bebas Neue&display=swap');
* {
/* border: 0; */
padding: 0;
margin: 0;
box-sizing: border-box;
color: #474B44;
/* background: #FEFDEB; */
}
body {
font-family: 'Bebas Neue', cursive;
color: #474B44;
letter-spacing: 1px;
background-color: #FEFDEC;
/* background-color: crimson; */
}
.nav {
padding: 30px;
background-color: #FDFBD8;
border-bottom: 1px solid #5a53530e;
display: flex;
justify-content: space-between;
align-items: center;
}
#login-btn {
padding: 10px 30px;
font-size: 25px;
background-color: rgb(127, 255, 148);
border: 2px solid #474B44;
border-radius: 5px;
}
#library {
font-size: 64px;
}
.new-book-btn {
font-size: 30px;
color: white;
background-color: #EEB868;
border: 2px solid #5a53530e;
border-radius: 5px;
padding: 10px 30px;
}
.bg-modal {
height: 100%;
width: 100%;
position: fixed;
background-color: rgba(0, 0, 0, 0.7);
justify-content: center;
align-items: center;
opacity: 0;
pointer-events: none;
display:flex;
transition: 200ms opacity;
}
.bg-modal.active {
opacity: 1;
pointer-events: auto;
}
.modal-content {
height: 600px;
width: 400px;
background-color: aliceblue;
padding: 40px;
border-radius: 5px;
transform: scale(0);
transition: 200ms transform;
}
.bg-modal.active .modal-content {
transform: scale(1);
}
.modal-form {
/* margin-top: 10px; */
display: flex;
flex-direction: column;
align-items: center;
gap: 30px;
border-radius: 5px;
justify-content: space-evenly;
}
.modal-dialouge {
font-size: 55px;
}
.modal-input {
width: 90%;
padding: 20px;
font-weight: bold;
font-size: 30px;
}
.modal-form-btn {
padding: 5px 10px;
background-color: yellow;
font-size: 30px;
border-radius: 5px;
font-family: 'Bebas Neue', cursive;
color: #474B44;
border: 2px solid #474B44;
}
.isRead {
display: flex;
gap: 20px;
font-size: 30px;
align-items: center;
}
.modal-checkbox {
width: 25px;
height: 25px;
}
<div >
<div >
<form action="">
<h2 >Add a new book</h2>
<input id="book" type="text" placeholder="Title">
<input id="pages" type="text" placeholder="Pages">
<input id="author" type="text" placeholder="Author">
<div >
<label for="readingStatus">Have you read it?</label>
<input type="checkbox" name="readingStatus" id="readingStatus">
</div>
<button type="button">New Book</button>
</form>
</div>
</div>
<header>
<div >
<h1 id="library">Ishaan's Library</h1>
<button id="login-btn">Login</button>
</div>
<div >
<button >Add Book</button>
</div>
</header>