I have been learning some simple CSS animations like this card flip, however when I create the next card element the animation seems to break and doesn't execute.
The animation itself is pretty basic, onclick the card should be flipped horizontally. I wonder if I have to create extra listener to 2nd card in order to animate it?
flipCard();
function flipCard() {
var card = document.querySelector('.card');
card.addEventListener('click', function() {
card.classList.toggle('is-flipped');
});
}
body {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
overflow: hidden;
color: #fff;
background: hsl(227, 10%, 10%);
min-height: 100vh;
min-width: 100vw;
}
.wrapper {
display: flex;
justify-content: space-evenly;
}
.scene {
width: 200px;
height: 260px;
border: 1px solid #CCC;
margin: 40px 0;
perspective: 600px;
}
.card {
width: 100%;
height: 100%;
transition: transform 1s;
transform-style: preserve-3d;
cursor: pointer;
position: relative;
}
.card.is-flipped {
transform: rotateY(180deg);
}
.cardFace {
position: absolute;
width: 100%;
height: 100%;
line-height: 260px;
color: white;
text-align: center;
font-weight: bold;
font-size: 40px;
backface-visibility: hidden;
}
.cardFaceFront {
background: red;
}
.cardFaceBack {
background: blue;
transform: rotateY(180deg);
}
<div class="wrapper">
<div class="scene">
<div class="card">
<div class="cardFace cardFaceFront">front</div>
<div class="cardFace cardFaceBack">back</div>
</div>
</div>
<div class="scene">
<div class="card">
<div class="cardFace cardFaceFront">front</div>
<div class="cardFace cardFaceBack">back</div>
</div>
</div>
</div>
Link to jsfiddle: https://jsfiddle.net/kapiko112/pokejcr4/