gpt4 book ai didi

javascript - 翻转所有卡片,但想一张一张地翻转

转载 作者:行者123 更新时间:2023-12-05 08:48:07 31 4
gpt4 key购买 nike

我试图一张一张地翻转这些卡片,但它们一起翻转。

我相信我的 javaScript 中遗漏了一些东西。

在原始代码中,我在前面使用图像,在后面使用无序列表,我尝试在此处恢复它,只写“Front”和“Back”。

$(".flip").click(function() {
$(".card").toggleClass("flipped");
});
.card-container {
display: flex;
}
.card {
width: 300px;
height: 6rem;
margin: 30px;
transform-style: preserve-3d;
transition: transform 1s;
}

.card figure {
margin: 0;
display: block;
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}

.card figure figcaption {
padding: 0 1rem;
backface-visibility: hidden;
border: 1px solid gray;
}

.card button.flip {
position: absolute;
right: 1rem;
margin: 0;
}

.card button.flip {
top: 1rem;
}

.card .back {
transform: rotateY( 180deg);
}

.card.flipped {
transform: rotateY( 180deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-container">
<div class="card">
<figure class="front">
<figcaption>
<h2>FRONT 1</h2>
<button class="flip">+</button>
</figcaption>
</figure>
<figure class="back">
<figcaption>
<h2>BACK 1</h2>
<button class="flip">-</button>
</figcaption>
</figure>
</div>

<div class="card">
<figure class="front">
<figcaption>
<h2>FRONT 2</h2>
<button class="flip">+</button>
</figcaption>
</figure>
<figure class="back">
<figcaption>
<h2>BACK 2</h2>
<button class="flip">-</button>
</figcaption>
</figure>
</div>
</div>

你们知道我做错了什么吗?

最佳答案

您可以使用 .closest() 方法仅翻转按钮所在的元素,而不是翻转类 .card 的所有元素 (它向上遍历 DOM 树,直到找到具有指定类的元素)

$(this).closest(".card").toggleClass("flipped");

$(".flip").click(function() {
$(this).closest(".card").toggleClass("flipped");
});
.card-container {
display: flex;
}
.card {
width: 300px;
height: 6rem;
margin: 30px;
transform-style: preserve-3d;
transition: transform 1s;
}

.card figure {
margin: 0;
display: block;
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}

.card figure figcaption {
padding: 0 1rem;
backface-visibility: hidden;
border: 1px solid gray;
}

.card button.flip {
position: absolute;
right: 1rem;
margin: 0;
}

.card button.flip {
top: 1rem;
}

.card .back {
transform: rotateY( 180deg);
}

.card.flipped {
transform: rotateY( 180deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-container">
<div class="card">
<figure class="front">
<figcaption>
<h2>FRONT 1</h2>
<button class="flip">+</button>
</figcaption>
</figure>
<figure class="back">
<figcaption>
<h2>BACK 1</h2>
<button class="flip">-</button>
</figcaption>
</figure>
</div>

<div class="card">
<figure class="front">
<figcaption>
<h2>FRONT 2</h2>
<button class="flip">+</button>
</figcaption>
</figure>
<figure class="back">
<figcaption>
<h2>BACK 2</h2>
<button class="flip">-</button>
</figcaption>
</figure>
</div>
</div>

关于javascript - 翻转所有卡片,但想一张一张地翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66449563/

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