卡翻转jquery同时翻转两张卡?

时间:2016-11-20 06:29:48

标签: javascript jquery html mobile card-flip

这是我第一次尝试卡片翻转编码。我得到了它的工作,但现在我有两件我想做的事情,我无法得到:

  1. 我希望每张卡片在单击相应的卡片时单独翻转。
  2. 我希望图片卡具有移动响应能力。
  3. function flip() {
      $('.card').toggleClass('flipped');
    }
    .cardContainer {
      width: 100%;
      height: 484px;
      position: relative;
      border: 1px solid #ccc;
      -webkit-perspective: 400px;
      -moz-perspective: 400px;
      -o-perspective: 400px;
      perspective: 400px;
      margin-bottom: 50px;
    }
    
    .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: 260px;
      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);
    }
    
    .card.flipped {
      -webkit-transform: rotateY( 180deg);
      -moz-transform: rotateY( 180deg);
      -o-transform: rotateY( 180deg);
      transform: rotateY( 180deg);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <section class="cardContainer">
      <div class="card" onclick="flip()">
        <div class="front"><img src="http://little.convio.net/images/content/pagebuilder/Alyssa_front.jpg" width="400" /></div>
        <div class="back">2</div>
      </div>
    </section>
    <section class="cardContainer">
      <div class="card" onclick="flip()">
        <div class="front"><img src="http://little.convio.net/images/content/pagebuilder/Alyssa_back.jpg" width="400" /></div>
        <div class="back">2</div>
      </div>
    </section>

    See jsFiddle

    Website where it's being used

    我不是网页设计师/开发人员,所以我的编码经验非常有限。这是完成此页面的最后一块拼图。我很感激帮助。

1 个答案:

答案 0 :(得分:0)

您可以在主脚本中管理点击事件,点击触发后,将toggle应用于距离事件目标最近的.card

$('document').ready(function() {
  $('.card').on('click', function(e) {
    $(e.target).closest('.card').toggleClass('flipped');
  });
});
.cardContainer {
  width: 100%;
  height: 484px;
  position: relative;
  border: 1px solid #ccc;
  -webkit-perspective: 400px;
  -moz-perspective: 400px;
  -o-perspective: 400px;
  perspective: 400px;
  margin-bottom: 50px;
}

.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: 260px;
  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);
}

.card.flipped {
  -webkit-transform: rotateY( 180deg);
  -moz-transform: rotateY( 180deg);
  -o-transform: rotateY( 180deg);
  transform: rotateY( 180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section class="cardContainer">
  <div class="card">
    <div class="front">
      <img src="http://little.convio.net/images/content/pagebuilder/Alyssa_front.jpg" width="400" />
    </div>
    <div class="back">2</div>
  </div>
</section>
<section class="cardContainer">
  <div class="card">
    <div class="front">
      <img src="http://little.convio.net/images/content/pagebuilder/Alyssa_back.jpg" width="400" />
    </div>
    <div class="back">2</div>
  </div>
</section>

对于响应部分,我建议您使用像960 Grid System或Bootstrap网格系统这样的网格系统。