尝试向模式添加 CSS 过渡,但未按预期工作

Trying to add a CSS transition to a modal, but it isn't working as expected

我正在尝试向我的模式添加 CSS 过渡。它应该可以工作,但事实并非如此。过渡应该在 200 毫秒内完成,但它没有发生。

我在下面附上了我的代码片段。我尝试了很多不同的东西,但结果是有效的。除了当我尝试使用代码检查器启用转换时,它按预期工作。我不知道问题出在哪里。

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 class="bg-modal">
  <div class="modal-content">
    <form class="modal-form" action="">
      <h2 class="modal-dialouge">Add a new book</h2>
      <input class="modal-input" id="book" type="text" placeholder="Title">
      <input class="modal-input" id="pages" type="text" placeholder="Pages">
      <input class="modal-input" id="author" type="text" placeholder="Author">
      <div class="isRead">
        <label for="readingStatus">Have you read it?</label>
        <input class="modal-checkbox" type="checkbox" name="readingStatus" id="readingStatus">
      </div>

      <button class="modal-form-btn" type="button">New Book</button>
    </form>
  </div>
</div>

<header>
  <div class="nav">
    <h1 id="library">Ishaan's Library</h1>
    <button id="login-btn">Login</button>
  </div>
  <div class="new-book">
    <button class="new-book-btn">Add Book</button>
  </div>
</header>

单击时,我们将该事件视为 e 并查看 target 的位置。目标是周围区域,而不是弹出窗口,我们将检查目标是否有 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 class="bg-modal">
  <div class="modal-content">
    <form class="modal-form" action="">
      <h2 class="modal-dialouge">Add a new book</h2>
      <input class="modal-input" id="book" type="text" placeholder="Title">
      <input class="modal-input" id="pages" type="text" placeholder="Pages">
      <input class="modal-input" id="author" type="text" placeholder="Author">
      <div class="isRead">
        <label for="readingStatus">Have you read it?</label>
        <input class="modal-checkbox" type="checkbox" name="readingStatus" id="readingStatus">
      </div>

      <button class="modal-form-btn" type="button">New Book</button>
    </form>
  </div>
</div>

<header>
  <div class="nav">
    <h1 id="library">Ishaan's Library</h1>
    <button id="login-btn">Login</button>
  </div>
  <div class="new-book">
    <button class="new-book-btn">Add Book</button>
  </div>
</header>