css3卡片翻转悬停和点击冲突

css3 card flip hover and click conflict

我正在使用 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 翻转效果仅在我们将光标从卡片上移开时有效!

效果如下

$(".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);
}
.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*/
<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>

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

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

$(".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>

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

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