I have successfully implemented a card flip effect using CSS3.
The card is flipped using jQuery and CSS
CSS
.flipped {
-webkit-transform: rotateY( 180deg );
-moz-transform: rotateY( 180deg );
-o-transform: rotateY( 180deg );
transform: rotateY( 180deg );
}
jQuery
<script>
$(".card").click(function () {
var id = $(this).attr('id');
$('#' + id).toggleClass('flipped');
});
Everything works fine but I encountered an issue when adding a hover effect like this:
.card:hover{
-webkit-transform: rotateY( 10deg );
-moz-transform: rotateY( 10deg );
-o-transform: rotateY( 10deg );
transform: rotateY( 100deg );
}
In this situation, the hover effect works correctly, but the onclick flip effect only triggers when moving the cursor away from the card!
Below is the code snippet demonstrating the effect
$(".card").click(function() {
var id = $(this).attr('id');
$('#' + id).toggleClass('flipped');
});
/*Card Flip*/
.card-container {
width: 150px;
height: 150px;
position: relative;
border: 1px solid #ccc;
-webkit-perspective: 800px;
-moz-perspective: 800px;
-o-perspective: 800px;
perspective: 800px;
float: left;
margin: 50px 50px 50px 50px;
cursor: pointer;
}
.card {
width: 100%;
height: 100%;
position: absolute;
-webkit-transition: -webkit-transform 1s;
-moz-transition: -moz-transform 1s;
-o-transition: -o-transform 1s;
transition: transform 1s;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-transform-origin: 50% 50%;
}
.card div {
display: block;
height: 100%;
width: 100%;
line-height: 150px;
color: white;
text-align: center;
font-weight: bold;
font-size: 140px;
position: absolute;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-o-backface-visibility: hidden;
backface-visibility: hidden;
}
.card .front {
background: red;
}
.card .back {
background: blue;
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
-o-transform: rotateY(180deg);
transform: rotateY(180deg);
}
.flipped {
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
-o-transform: rotateY(180deg);
transform: rotateY(180deg);
}
.card:hover {
-webkit-transform: rotateY(20deg);
-moz-transform: rotateY(20deg);
-o-transform: rotateY(20deg);
transform: rotateY(20deg);
}
.flipped:hover {
-webkit-transform: rotateY(20deg);
-moz-transform: rotateY(20deg);
-o-transform: rotateY(20deg);
transform: rotateY(20deg);
}
/*Card Flip*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<section class="card-container">
<div class="card" id="card1">
<div class="front">1</div>
<div class="back">2</div>
</div>
</section>