Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flipping two different images and stopping on hover

Tags:

html

css

The idea is to create an infinite animation on CSS that will show two sides of a card spinning all the time and to stop the animation on hover, revealing only the front and increasing the size 20% with a link to another section.

I am able to flip and grow to the second image on hover, but I seem to be unable to replace the action on a Keyframe animation.

This is what I have so far:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.panel {
    width: 300px;
    height: 300px;
    margin: auto;
    position: relative;
}
.card {
    width: 100%;
    height: 100%;
    -o-transition: all .5s;
    -ms-transition: all .5s;
    -moz-transition: all .5s;
    -webkit-transition: all .5s;
    transition: all .5s;
    -webkit-backface-visibility: hidden;
    -ms-backface-visibility: hidden;
    -moz-backface-visibility: hidden;
    backface-visibility: hidden;
    position: absolute;
    top: 0px;
    left: 0px;
    -webkit-animation: CardFlip 5s infinite;
}
.front {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 2;
    background-image: url('http://placehold.it/300x300/red');

}
.back {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
    -webkit-transform: rotateY(-180deg);
    -ms-transform: rotateY(-180deg);
    -moz-transform: rotateY(-180deg);  
    transform: rotateY(-180deg);  
    background-image: url('http://placehold.it/300x300/blue');

}
.panel:hover .front {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
    -webkit-transform: rotateY(180deg);
    -ms-transform: rotateY(180deg);
    -moz-transform: rotateY(180deg);
    transform: rotateY(180deg);
}
.panel:hover .back {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 2;   
    -webkit-transform: rotateY(0deg);
    -ms-transform: rotateY(0deg);
    -moz-transform: rotateY(0deg);
    transform: rotateY(0deg);
    -webkit-transform: scale(1.2,1.2);
    -ms-transform: scale(1.2,1.2);
    -moz-transform:  scale(1.2,1.2);
    transform: scale(1.2,1.2);
}

@-webkit-keyframes CardFlip {
  0%   { 
position: absolute;
    top: 0px;
    left: 0px;
    z-index: 2;   
    -webkit-transform: rotateY(0deg);
    -ms-transform: rotateY(0deg);
    -moz-transform: rotateY(0deg);
    transform: rotateY(0deg);
    -webkit-transform: scale(1.2,1.2);
    -ms-transform: scale(1.2,1.2);
    -moz-transform:  scale(1.2,1.2);
    transform: scale(1.2,1.2);
  }
  100% { 
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
    -webkit-transform: rotateY(180deg);
    -ms-transform: rotateY(180deg);
    -moz-transform: rotateY(180deg);
    transform: rotateY(180deg);
  }
}
</style>
</head>
<body>
<br>        
<br>    
<div class="panel">
    <div class="front card">
    </div>
    <div class="back card">  
    </div>
</div>
</body>
like image 684
JFD Avatar asked Nov 10 '22 05:11

JFD


1 Answers

I think you need to add backface-visibility: hidden; only to front card.

If you need your animation to have infinite look, you have to have similar 0% and 100% points.

Also, you had missed transform-style: preserve-3d; property.

Also, I've added one more container to avoid animation on cards. I think it's more semantic.

Check out this fiddle: http://jsfiddle.net/nikoloza/2zrk7/

Update

If we add perspective: 1000 to the main container, we'll get real 3d effect. Fiddle: http://jsfiddle.net/nikoloza/2zrk7/1/

Update 2

And if we need only left-to-right flipping we can set 360deg instead of 0deg into 100% point in the animation. Fiddle: http://jsfiddle.net/nikoloza/2zrk7/2/

like image 150
nikoloza Avatar answered Nov 12 '22 17:11

nikoloza