Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
Static cards won't flip when "flip" function called

I have been trying for hours to get my playing cards to flip when the player clicks on them. When the eventListener is fired it should run the flip function which toggles between the front/backsides of a card. What am I doing wrong?

const playingCards = document.querySelectorAll('.playing-cards');

function flipCard(){;
    this.classList.toggle("flip")
}

playingCards.forEach((card) => card.addEventListener("click", flipCard));
.playing-cards{
    display: inline-block;
    padding: 20px;
    position: relative;
    cursor: pointer;
}
.playing-cards .flip{
    transform: rotateY(180deg);
}
.front-card{
    background-color: aqua;
    width: 300px;
    height: 300px;
    transform: rotateX(0deg);
}
.back-card{
    background-color: greenyellow; 
    width: 300px;
    height: 300px;
    transform: rotateY(180deg);
}
.front-card,
.back-card{
    width: 100%;
    height: 100%;
    position: absolute;
    backface-visibility: hidden;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="index.css">
    <title>Document</title>
</head>

<div class="memory-game">
    <div class="playing-cards">
        <div class="front-card">A</div>
        <div class="back-card"></div>
    </div>

    <div class="playing-cards">
        <div class="front-card">B</div>
        <div class="back-card"></div>
    </div>

    <div class="playing-cards">
        <div class="front-card">a</div>
        <div class="back-card"></div>
    </div>

    <div class="playing-cards">
        <div class="front-card">b</div>
        <div class="back-card"></div>
    </div>


</div>


<body>
    <script src=index.js></script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Answer derived from here

const playingCards = document.querySelectorAll('.playing-cards');

function flipCard() {
  this.classList.toggle("flip")
}

playingCards.forEach((card) => card.addEventListener("click", flipCard));
/* The flip card container - set the width and height to whatever you want. We have added the border property to demonstrate that the flip itself goes out of the box on hover (remove perspective if you don't want the 3D effect */
.memory-game {
  background-color: transparent;
  border: 1px solid #f1f1f1;
  perspective: 1000px; /* Remove this if you don't want the 3D effect */
}

/* This container is needed to position the front and back side */
.playing-cards {
display: inline-block;
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
  cursor: pointer;/*clickable-cursor*/
}

/* Do an horizontal flip when you move the mouse over the flip box container */
.playing-cards.flip {
  transform: rotateY(180deg);
}

/* Position the front and back side */
.front-card, .back-card {
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden; /* Safari */
  backface-visibility: hidden;
}

/* Style the front side (fallback if image is missing) */
.front-card {
  background-color: aqua;

  color: black;
}

/* Style the back side */
.back-card {
  background-color: greenyellow;
  color: white;
  transform: rotateY(180deg);
}
<div class="memory-game">
  <div class="playing-cards">
    <div class="front-card">A</div>
    <div class="back-card"></div>
  </div>

  <div class="playing-cards">
    <div class="front-card">B</div>
    <div class="back-card"></div>
  </div>

  <div class="playing-cards">
    <div class="front-card">a</div>
    <div class="back-card"></div>
  </div>

  <div class="playing-cards">
    <div class="front-card">b</div>
    <div class="back-card"></div>
  </div>
</div>

Edit: Added cursor effect

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda