gpt4 book ai didi

javascript - 一次 3D 翻转多张卡片

转载 作者:太空宇宙 更新时间:2023-11-04 09:30:41 24 4
gpt4 key购买 nike

我正在尝试理解文章中的 3D 卡片动画翻转功能:http://callmenick.com/post/css-transitions-transforms-animations-flipping-card

但我不知道如何在单击时一次翻转 2 张卡片。例如,如果我点击第一张牌,我也想同时翻转第二张牌。

HTML:

<div class="card effect__click">
<div class="card__front">
<span class="card__text">front</span>
</div>
<div class="card__back">
<span class="card__text">back</span>
</div>
</div>

CSS:

.card {
position: relative;
float: left;
padding-bottom: 25%;
width: 25%;
text-align: center;
}

.card__front,
.card__back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

.card__front,
.card__back {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transition: -webkit-transform 0.9s;
transition: transform 0.9s;
}
.card__text{color:#fff;}
.card__front {
background-color: #ff5078;
}

.card__back {
background-color: #1e1e1e;
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}
.card.effect__hover:hover .card__front {
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}

.card.effect__hover:hover .card__back {
-webkit-transform: rotateY(0);
transform: rotateY(0);
}
.card.effect__click.flipped .card__front {
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}

.card.effect__click.flipped .card__back {
-webkit-transform: rotateY(0);
transform: rotateY(0);
}

JS:

(function() {
var cards = document.querySelectorAll(".card.effect__click");
for ( var i = 0, len = cards.length; i < len; i++ ) {
var card = cards[i];
clickListener(card);
}

function clickListener(card) {
card.addEventListener( "click", function() {
var c = this.classList;
c.contains("flipped") === true ? c.remove("flipped") : c.add("flipped");
});
}
})();

最佳答案

或者querySelector[All]对于使用 :nth-child 的下一个元素选择器或其他合适的选择器或使用 nextElementSibling :

(function() {
var cards = document.querySelectorAll(".card.effect__click");
for ( var i = 0, len = cards.length; i < len; i++ ) {
var card = cards[i];
clickListener(card);
}

function flip(card){
if (!card) return;
var c = card.classList;
c.contains("flipped") === true ? c.remove("flipped") : c.add("flipped");
}

function clickListener(card) {
card.addEventListener( "click", function() {
flip(this);
flip(this.nextElementSibling);
});
}
})();
.card {
position: relative;
float: left;
padding-bottom: 25%;
width: 25%;
text-align: center;
}

.card__front,
.card__back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

.card__front,
.card__back {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transition: -webkit-transform 0.9s;
transition: transform 0.9s;
}
.card__text{color:#fff;}
.card__front {
background-color: #ff5078;
}

.card__back {
background-color: #1e1e1e;
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}
.card.effect__hover:hover .card__front {
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}

.card.effect__hover:hover .card__back {
-webkit-transform: rotateY(0);
transform: rotateY(0);
}
.card.effect__click.flipped .card__front {
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}

.card.effect__click.flipped .card__back {
-webkit-transform: rotateY(0);
transform: rotateY(0);
}
<div class="card effect__click">
<div class="card__front">
<span class="card__text">front</span>
</div>
<div class="card__back">
<span class="card__text">back</span>
</div>
</div>
<div class="card effect__click">
<div class="card__front">
<span class="card__text">front</span>
</div>
<div class="card__back">
<span class="card__text">back</span>
</div>
</div>

关于javascript - 一次 3D 翻转多张卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40818436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com