我将使用此代码在卡片的正面和背面旋转,但是单击一次后,我会看到背面卡片,而第二次单击后,我看不到任何卡片!问题是什么?

$(".carta img").click(function() {
  $(this).toggleClass("flipped");
})
.contenitorecarta {
  position: relative;
  width: 100px;
  height: 150px;
  perspective: 800px;
}
.carta {
  width: 100px;
  height: 150px;
  position: absolute;
  transform-style: preserve-3d;
  transition: transform 1s;
}
.carta img {
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
}
.carta.back {
  transform: rotateY(180deg)
}
.carta .flipped {
  transform: rotateY(180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="contenitore-carta">
  <div class="carta">
    <div class="front">
      <img width="100" height="150" src="http://placehold.it/100x150/F44/000.png&text=Front">
    </div>
    <div class="back">
      <img width="100" height="150" src="http://placehold.it/100x150/44F/000.png&text=Back">
    </div>
  </div>
</div>

最佳答案

我假设您正在关注本教程?:http://desandro.github.io/3dtransforms/docs/card-flip.html

问题

您有四(4)个问题:

  • 在CSS中,您应该...
  • .contenitore-carta而不是.contenitorecarta
  • .carta.flipped代替.carta .flipped
  • .carta .back代替.carta.back
  • 在您的JavaScript中,以下内容应更改为...
  • $(".carta img")$(".carta")

  • 另外,您需要添加供应商前缀的样式规则,以便转换可以在所有受支持的浏览器中使用。有关更多信息,请参见A List Apart: Prefix or Posthack



    下面的代码应正常工作。 注意:我将类(class)名称从意大利语翻译成英语:)

    $(".card").click(function() {
      $(this).toggleClass("flipped");
    })
    .container {
      width: 100px;
      height: 150px;
      position: relative;
      border: 1px solid #CCC;
      -webkit-perspective: 800px;
         -moz-perspective: 800px;
           -o-perspective: 800px;
              perspective: 800px;
    }
    
    .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;
    }
    
    .card.flipped {
      -webkit-transform: rotateY(180deg);
         -moz-transform: rotateY(180deg);
           -o-transform: rotateY(180deg);
              transform: rotateY(180deg);
    }
    
    .card div {
      display: block;
      height: 100%;
      width: 100%;
      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);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="container">
      <div class="card">
        <div class="front">
          <img width="100" height="150" src="http://placehold.it/100x150/F44/000.png&text=Front">
        </div>
        <div class="back">
          <img width="100" height="150" src="http://placehold.it/100x150/44F/000.png&text=Back">
        </div>
      </div>
    </div>

    10-05 22:22