【问题标题】:Trying to add a CSS transition to a modal, but it isn't working as expected尝试向模态添加 CSS 过渡,但没有按预期工作
【发布时间】:2022-01-03 19:30:45
【问题描述】:

我正在尝试向我的模态添加 CSS 过渡。它应该工作,但它不是。转换应该在 200 毫秒内完成,但它没有发生。

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

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>

【问题讨论】:

    标签: css css-transitions


    【解决方案1】:

    当被点击时,我们会将该事件作为e 并查看target 的位置。目标是周围区域,而不是弹出窗口,我们将检查目标是否具有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>

    【讨论】:

    • 哦,我打算在找到 CSS 过渡的解决方案后解决这个问题。我正在尝试使用“transform: scale(1)”来缩放弹出窗口。我在“.modal-content”中添加了过渡属性,但它不起作用。这就是问题所在,我正在努力解决。
    • @Roarke 也修复了这个问题,可能有一些不必要的代码,你应该检查并删除它们
    猜你喜欢
    • 2014-03-30
    • 1970-01-01
    • 2015-10-12
    • 2021-04-05
    • 2023-04-05
    • 2021-08-08
    • 1970-01-01
    • 2013-12-01
    相关资源
    最近更新 更多