为什么我的翻转动画不适用于chrome和safari?

时间:2016-09-16 02:35:27

标签: javascript css cross-browser

我的第一次发帖。 对不起,如果我的代码有点乱,我仍然是初学者,我一直在尝试使这个翻转动画在chrome和safari上运行。它适用于Firefox,但我无法弄清楚为什么翻转动画在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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

在背面设置一个z-index,删除相同的id并使其成为一个类。

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>

之后你会说你不能点击背面翻转。这与backface-visibility: hidden;规则有关。