淡入淡出元素的过渡不起作用

Transition to fade an element doesn't work

我的代码有问题,可能有人知道解决方案。

我创建了一些卡片,我希望当我点击其中一张时,上面会出现一张更大的新卡片。我用 for 循环很容易做到,但问题是过渡,我想设置一个平滑的过渡。

大卡的初始状态是display: none;opacity: 0; 我知道我们不能淡化显示 none 元素,但我的想法是先设置 display: block 然后 opacity: 1 并在 css 中设置过渡 属性在“卡片打开”元素中。

但是转换不起作用,这让我很抓狂,因为我不知道为什么

我在这里分享代码笔 link : https://codepen.io/arnaudhrt/pen/VwWBLze

和javascript代码:

const Card = document.querySelectorAll('.card')
const CardOpen = document.querySelectorAll('.card-open')
const btnClose = document.querySelectorAll('.close')


for(let i = 0 ; i < Card.length ; i++) {
  Card[i].addEventListener('click', function() {
    CardOpen[i].style.display = 'block'
    CardOpen[i].style.opacity = '1'
  });
  btnClose[i].addEventListener('click', () => {
    CardOpen[i].style.opacity = '0'
    CardOpen[i].style.display = 'none'
  })
}

PS:我无法设置显示块,只能切换不透明度,因为如果我这样做,我就无法将其他卡片与 z-index 一起使用。

您可以使用可见性代替显示,显示在 css 中没有过渡效果。

const Card = document.querySelectorAll('.card')
const CardOpen = document.querySelectorAll('.card-open')
const btnClose = document.querySelectorAll('.close')


for(let i = 0 ; i < Card.length ; i++) {
  Card[i].addEventListener('click', function() {
    CardOpen[i].style.visibility = 'visible'
    CardOpen[i].style.opacity = '1'
  });
  btnClose[i].addEventListener('click', () => {
    CardOpen[i].style.opacity = '0'
    CardOpen[i].style.visibility = 'hidden'
  })
}
body {
  background: #565279;
    width: 100vw;
    height: 100vh;
    display: flex;
}
.container-card {
    max-width: 1200px;
    margin: auto;
    display: flex;
    flex-wrap: wrap;
    align-content: center;
    position: relative;
    transition: all 0.4s ease-in-out;
}
.card {
    width: 200px;
    height: 200px;
    background: crimson;
    margin: 20px;
    cursor: pointer;
    transition: all 0.4s ease-in-out;
}
.card-open {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: #333;
    visibility: hidden;
    opacity: 0;
    transition: all 0.4s ease-in-out;
}
.card-open span {
    color: #fff;
    position: absolute;
    bottom: 30px;
    left: 50%;
    transform:translateX(-50%);
    cursor: pointer;
}
<div class="container-card">
      <div class="card card1"></div>
      <div class="card card2"></div>
      <div class="card card3"></div>
      <div class="card card4"></div>
      <div class="card card5"></div>
      <div class="card card6"></div>
      <div class="card card7"></div>
      <div class="card card8"></div>
      <div class="card card9"></div>
      <div class="card card10"></div>
      <div class="card-open open1"><span class="close">close</span></div>
      <div class="card-open open2"><span class="close">close</span></div>
      <div class="card-open open3"><span class="close">close</span></div>
      <div class="card-open open4"><span class="close">close</span></div>
      <div class="card-open open5"><span class="close">close</span></div>
      <div class="card-open open6"><span class="close">close</span></div>
      <div class="card-open open7"><span class="close">close</span></div>
      <div class="card-open open8"><span class="close">close</span></div>
      <div class="card-open open9"><span class="close">close</span></div>
      <div class="card-open open10"><span class="close">close</span></div>
    </div>