css 为什么我的.invisible类被添加到所有元素中-子元素、父元素等

0x6upsns  于 2023-02-06  发布在  其他
关注(0)|答案(2)|浏览(206)

当我拖动一张红色边框的卡片时,我想只显示拖动的元素,所以,我给原来的元素添加了. invisible类,但是现在所有的东西都是不可见的,为什么?
完整代码:https://codepen.io/asamad9134/pen/WNKLpZb

const cards = document.querySelector('.cards');

cards.addEventListener('dragstart', (event) => {
  const selectedCard = event.target;
  selectedCard.classList.add('invisible');
});

cards.addEventListener('dragend', (event) => {
  const selectedCard = event.target;
  selectedCard.classList.remove('invisible');
});
.cards {
  display: flex;
  width: 100vw;
  justify-content: space-around;
}

.card {
  font-family: Arabic;
  border-radius: 10%;
  ;
  font-size: 4em;
  text-align: center;
  width: 200px;
  height: 130px;
  background-color: white;
  border: 10px solid rgb(255, 85, 0);
}

.card:hover {
  cursor: grab;
  transform: scale(1.2);
  transition: 0.3s;
}

.empty-divs {
  display: flex;
  justify-content: space-around;
  width: 100vw;
}

.empty-div {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.blank-card {
  width: 200px;
  height: 130px;
  background-color: #f4f4f4;
  border-radius: 10%;
  text-align: center;
  border: 10px solid grey;
}

.invisible {
  display: none;
}
<section class="cards">
  <p class="card card-idle" id="أَرْنَب" draggable=true>أَرْنَب</p>
  <p class="card card-idle" draggable=true>كِتَاب</p>
  <p class="card card-idle" draggable=true>كُرَة</p>
</section>

<section class="empty-divs">

  <div class="empty-div">
    <img id="rabbit" src="http://source.unsplash.com/random/200x200" alt="rabbit" />
    <p class="blank-card"></p>

  </div>

  <div class="empty-div">
    <img id="rabbit" src="http://source.unsplash.com/random/200x200" alt="rabbit" />
    <p class="blank-card"></p>
  </div>

  <div class="empty-div">
    <img id="rabbit" src="http://source.unsplash.com/random/200x200" alt="rabbit" />
    <p class="blank-card"></p>
  </div>

</section>

编辑,我设置了不透明度:0;但这有一个奇怪的滞后效应。

e4yzc0pl

e4yzc0pl1#

display: none;会删除标记及其效果,但标记在源代码中仍然可见。请尝试使用opacity: 0.4;,以便更改标记的可见性,而不是将其从结构中完全删除。如果希望标记完全不可见,但不删除,则可以将不透明度设置为0。

hgncfbus

hgncfbus2#

这里有一种方法可以使它只显示当前被拖动的元素。注意,我还使用了visibility: hidden来隐藏卡片,而不是opacity: 0

// create a nodelist of all of the cards
const cards = document.querySelectorAll('.card');

// add a an event listener for each card in the nodelist
cards.forEach(card => {
  card.addEventListener('dragstart', (event) => { 
    const selectedCard = event.target;
    // create an array of the cards that are not selected so you can
    // "hide" only the not selected cards
    const notSelectedCards = Array.from(cards).filter(card => card !== selectedCard)
    selectedCard.classList.add('card-dragging');
    notSelectedCards.forEach(card => card.classList.add('invisible'));
    
    // remove the "invisible" class (and do whatever other
    // cleanup on dragend that you want)
    selectedCard.addEventListener('dragend', (event) => { 
      selectedCard.classList.remove('card-dragging');
      notSelectedCards.forEach(card => card.classList.remove('invisible'));
    }, { once: true});
  })
})
* {
  box-sizing: border-box;
}

.cards {
  display:flex;
  justify-content: space-around;
}

.card {
  font-family: Arabic;
  border-radius: 10%;
  font-size: 4em;
  text-align:center;
  width: 200px;
  height: 130px;
  background-color: white;  
  border: 10px solid rgb(255, 85, 0);
}

.card:hover{
  cursor: grab;
  transform: scale(1.2);
  transition: 0.3s;
}

.empty-divs{
  display: flex;
  justify-content: space-around;
}

.empty-div {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.blank-card {
  width: 200px;
  height: 130px;
  background-color: #f4f4f4;
  border-radius: 10%;
  text-align:center;
  border: 10px solid grey;
}

.invisible {
  visibility: hidden;
}

.card-dragging {
  opacity: .2;
}
<section class="cards">
  <p class="card card-idle" id="أَرْنَب" draggable=true>أَرْنَب</p>
  <p class="card card-idle" draggable=true>كِتَاب</p>
  <p class="card card-idle" draggable=true>كُرَة</p>
</section>

<section class="empty-divs">

  <div class="empty-div">
    <img src="http://source.unsplash.com/random/200x200" alt="random" />
    <p class="blank-card"></p>

  </div>

  <div class="empty-div">
    <img src="http://source.unsplash.com/random/200x200" alt="random" />
    <p class="blank-card"></p>
  </div>

  <div class="empty-div">
    <img src="http://source.unsplash.com/random/200x200" alt="random" />
    <p class="blank-card"></p>
  </div>

</section>

相关问题