css3卡翻转悬停并单击冲突

时间:2015-01-23 07:37:52

标签: jquery html css css3 css-transitions

我正在使用css3创建卡片翻转效果。

我正在使用jQuery和css

翻转卡片

CSS

    .flipped {
    -webkit-transform: rotateY( 180deg );
    -moz-transform: rotateY( 180deg );
    -o-transform: rotateY( 180deg );
    transform: rotateY( 180deg );
}

的jQuery

<script>
$(".card").click(function () {
    var id = $(this).attr('id');
    $('#' + id).toggleClass('flipped');
});

它的工作正常,但是我已经应用了像

这样的悬停效果
.card:hover{
    -webkit-transform: rotateY( 10deg );
    -moz-transform: rotateY( 10deg );
    -o-transform: rotateY( 10deg );
    transform: rotateY( 100deg );
}

在这种情况下,悬停效果正常,但onclick翻转效果仅在我们从卡上移动光标时才有效!

这是效果

&#13;
&#13;
$(".card").click(function() {
  var id = $(this).attr('id');
  $('#' + id).toggleClass('flipped');
});
&#13;
/*Card Flip*/

.card-container {
  width: 150px;
  height: 150px;
  position: relative;
  border: 1px solid #ccc;
  -webkit-perspective: 800px;
  -moz-perspective: 800px;
  -o-perspective: 800px;
  perspective: 800px;
  float: left;
  margin: 50px 50px 50px 50px;
  cursor: pointer;
}
.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: 150px;
  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);
}
.flipped {
  -webkit-transform: rotateY(180deg);
  -moz-transform: rotateY(180deg);
  -o-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
.card:hover {
  -webkit-transform: rotateY(20deg);
  -moz-transform: rotateY(20deg);
  -o-transform: rotateY(20deg);
  transform: rotateY(20deg);
}
.flipped:hover {
  -webkit-transform: rotateY(20deg);
  -moz-transform: rotateY(20deg);
  -o-transform: rotateY(20deg);
  transform: rotateY(20deg);
}
/*Card Flip*/
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<section class="card-container">
  <div class="card" id="card1">
    <div class="front">1</div>
    <div class="back">2</div>
  </div>
</section>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

我相信你:hover .flipped可以旋转到20degY,所以当你将鼠标悬停在它上面时,我认为它应该做的事情。

有一件事就是替换.flipped:hover,并将其与类.flipped连接起来,这样当它悬停时它会旋转到180度,当它应用了该类时,如果这是预期的行为。

$(".card").click(function() {
  var id = $(this).attr('id');
  $('#' + id).toggleClass('flipped');
});
/*Card Flip*/

.card-container {
  width: 150px;
  height: 150px;
  position: relative;
  border: 1px solid #ccc;
  -webkit-perspective: 800px;
  -moz-perspective: 800px;
  -o-perspective: 800px;
  perspective: 800px;
  float: left;
  margin: 50px 50px 50px 50px;
  cursor: pointer;
}
.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: 150px;
  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:hover {
  -webkit-transform: rotateY(20deg);
  -moz-transform: rotateY(20deg);
  -o-transform: rotateY(20deg);
  transform: rotateY(20deg);
}
.flipped, .flipped:hover {/*notice here*/
  -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.0/jquery.min.js"></script>
<section class="card-container">
  <div class="card" id="card1">
    <div class="front">1</div>
    <div class="back">2</div>
  </div>
</section>

答案 1 :(得分:1)

翻转的卡实际上应该有200deg,而不是20.这是jsFiddle示例,代码为:http://jsfiddle.net/zbgnggkp/2/

.flipped:hover {
    -webkit-transform: rotateY(200deg);
    -moz-transform: rotateY(200deg);
    -o-transform: rotateY(200deg);
    transform: rotateY(200deg);
}