I am trying to flip these cards one by one, but they are flipping all together.
I believe I am missing something on my javaScript.
On the original code I am using images in front and an unordered list in back, I tried to resume it here writing only “Front” and “Back”.
JavaScript
x
3
1
$(".flip").click(function() {
2
$(".card").toggleClass("flipped");
3
});
JavaScript
1
43
43
1
.card-container {
2
display: flex;
3
}
4
.card {
5
width: 300px;
6
height: 6rem;
7
margin: 30px;
8
transform-style: preserve-3d;
9
transition: transform 1s;
10
}
11
12
.card figure {
13
margin: 0;
14
display: block;
15
position: absolute;
16
width: 100%;
17
height: 100%;
18
backface-visibility: hidden;
19
}
20
21
.card figure figcaption {
22
padding: 0 1rem;
23
backface-visibility: hidden;
24
border: 1px solid gray;
25
}
26
27
.card button.flip {
28
position: absolute;
29
right: 1rem;
30
margin: 0;
31
}
32
33
.card button.flip {
34
top: 1rem;
35
}
36
37
.card .back {
38
transform: rotateY( 180deg);
39
}
40
41
.card.flipped {
42
transform: rotateY( 180deg);
43
}
JavaScript
1
32
32
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div class="card-container">
3
<div class="card">
4
<figure class="front">
5
<figcaption>
6
<h2>FRONT 1</h2>
7
<button class="flip">+</button>
8
</figcaption>
9
</figure>
10
<figure class="back">
11
<figcaption>
12
<h2>BACK 1</h2>
13
<button class="flip">-</button>
14
</figcaption>
15
</figure>
16
</div>
17
18
<div class="card">
19
<figure class="front">
20
<figcaption>
21
<h2>FRONT 2</h2>
22
<button class="flip">+</button>
23
</figcaption>
24
</figure>
25
<figure class="back">
26
<figcaption>
27
<h2>BACK 2</h2>
28
<button class="flip">-</button>
29
</figcaption>
30
</figure>
31
</div>
32
</div>
Do you guys know what am I doing wrong?
Advertisement
Answer
Instead of flipping all elements of class .card
, you can flip only the one that the button is in, using the .closest()
method (which traverses UP the DOM tree until it finds an element with the specified class).
JavaScript
1
2
1
$(this).closest(".card").toggleClass("flipped");
2
JavaScript
1
3
1
$(".flip").click(function() {
2
$(this).closest(".card").toggleClass("flipped");
3
});
JavaScript
1
43
43
1
.card-container {
2
display: flex;
3
}
4
.card {
5
width: 300px;
6
height: 6rem;
7
margin: 30px;
8
transform-style: preserve-3d;
9
transition: transform 1s;
10
}
11
12
.card figure {
13
margin: 0;
14
display: block;
15
position: absolute;
16
width: 100%;
17
height: 100%;
18
backface-visibility: hidden;
19
}
20
21
.card figure figcaption {
22
padding: 0 1rem;
23
backface-visibility: hidden;
24
border: 1px solid gray;
25
}
26
27
.card button.flip {
28
position: absolute;
29
right: 1rem;
30
margin: 0;
31
}
32
33
.card button.flip {
34
top: 1rem;
35
}
36
37
.card .back {
38
transform: rotateY( 180deg);
39
}
40
41
.card.flipped {
42
transform: rotateY( 180deg);
43
}
JavaScript
1
32
32
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div class="card-container">
3
<div class="card">
4
<figure class="front">
5
<figcaption>
6
<h2>FRONT 1</h2>
7
<button class="flip">+</button>
8
</figcaption>
9
</figure>
10
<figure class="back">
11
<figcaption>
12
<h2>BACK 1</h2>
13
<button class="flip">-</button>
14
</figcaption>
15
</figure>
16
</div>
17
18
<div class="card">
19
<figure class="front">
20
<figcaption>
21
<h2>FRONT 2</h2>
22
<button class="flip">+</button>
23
</figcaption>
24
</figure>
25
<figure class="back">
26
<figcaption>
27
<h2>BACK 2</h2>
28
<button class="flip">-</button>
29
</figcaption>
30
</figure>
31
</div>
32
</div>