MikeDee MikeDee - 2 months ago 20
CSS Question

Why does my flip animation not work in chrome and safari?

My first time post.
Sorry if my code is a bit messy, I am still a beginner and I have been trying to make this flip animation work on chrome and safari. It works on firefox but I cannot figure out why the flip animation doesnt work correctly on chrome.



function flip(){
document.getElementById('card').classList.toggle('flip');
}

function flip2(){
document.getElementById('card2').classList.toggle('flip');
}

function flip3(){
document.getElementById('card3').classList.toggle('flip');
}

h4{
font-size: 2em;
}

#week-wrap {
height: auto;
max-width: 100%;
width: auto;
background-color: #efefef;
text-align: center;
}


#card, #card2, #card3 {
text-align: center;
position: relative;
display: inline-block;
max-width: 30%;
cursor:pointer;
margin: 1%;
-webkit-transition: all 1s ease;
transition: all 1s ease;

-webkit-transform-style: preserve-3d;
transform-style: preserve-3d;

-webkit-backface-visibility: hidden;
backface-visibility: hidden;


border-style: solid;

}

#back{
top:0;
left:0;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;


position: absolute;

color:white;
background-color: teal;
width: 100%;
height: 100%;
}

#back{
-webkit-transform: rotateY(180deg);
transform: rotateY(180deg);


}


.figcaption {
text-align: center;
width:100%;
height:0;
font-size: 4em;
line-height: 0;
color:white;
position:absolute;
top:10%;
z-index:1;
}

#card img {
-webkit-display:block;
display:block;

max-width: 100%;
}

#card2 img {
display:block;
max-width: 100%;
}

#card3 img {
display:block;
max-width: 100%;
}

.flip{
-webkit-transform: rotateY(180deg);
transform: rotateY(180deg);


}

<div id="week-wrap">

<div id="card" onclick="flip()">
<div class="figcaption">Front</div>
<img src="http://www.petakids.com/wp-content/uploads/2015/11/Baby-Bunny.jpg" />
<div id="back"><h3>Back</h3>
</div>

</div>

<div id="card2" onclick="flip2()">
<div class="figcaption">Front </div>
<img src="http://www.petakids.com/wp-content/uploads/2015/11/Baby-Bunny.jpg" />
<div id="back"><h3>Back</h3>
</div>


</div>

<div id="card3" onclick="flip3()">
<div class="figcaption">Front </div>
<img src="http://www.petakids.com/wp-content/uploads/2015/11/Baby-Bunny.jpg" />
<div id="back"><h3>Back</h3> </div>
</div>
</div>




Answer

Set a z-index on back, get rid of the same id and make it a class.

function flip() {
  document.getElementById('card').classList.toggle('flip');
}

function flip2() {
  document.getElementById('card2').classList.toggle('flip');
}

function flip3() {
  document.getElementById('card3').classList.toggle('flip');
}
h4 {
  font-size: 2em;
}
#week-wrap {
  height: auto;
  max-width: 100%;
  width: auto;
  background-color: #efefef;
  text-align: center;
}
#card,
#card2,
#card3 {
  text-align: center;
  position: relative;
  display: inline-block;
  max-width: 30%;
  cursor: pointer;
  margin: 1%;
  -webkit-transition: all 1s ease;
  transition: all 1s ease;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  border-style: solid;
}
.back {
  top: 0;
  left: 0;
  z-index: 1;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  position: absolute;
  color: white;
  background-color: teal;
  width: 100%;
  height: 100%;
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
.figcaption {
  text-align: center;
  width: 100%;
  height: 0;
  font-size: 4em;
  line-height: 0;
  color: white;
  position: absolute;
  top: 10%;
  z-index: 1;
}
#card img {
  -webkit-display: block;
  display: block;
  max-width: 100%;
}
#card2 img {
  display: block;
  max-width: 100%;
}
#card3 img {
  display: block;
  max-width: 100%;
}
.flip {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
<div id="week-wrap">
  <div id="card" onclick="flip()">
    <div class="figcaption">Front</div>
    <img src="http://www.petakids.com/wp-content/uploads/2015/11/Baby-Bunny.jpg" />
    <div class="back">
      <h3>Back</h3>
    </div>
  </div>
  <div id="card2" onclick="flip2()">
    <div class="figcaption">Front</div>
    <img src="http://www.petakids.com/wp-content/uploads/2015/11/Baby-Bunny.jpg" />
    <div class="back">
      <h3>Back</h3>
    </div>
  </div>
  <div id="card3" onclick="flip3()">
    <div class="figcaption">Front</div>
    <img src="http://www.petakids.com/wp-content/uploads/2015/11/Baby-Bunny.jpg" />
    <div class="back">
      <h3>Back</h3> 
    </div>
  </div>
</div>

And after that you are going to say that you can not click the backside to flip back. That has something to do with the backface-visibility: hidden; rule.

Comments