Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda