-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (52 loc) · 2.23 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
document.addEventListener("DOMContentLoaded", function() {
let cardsContainer = document.querySelector(".cards-container");
let cards = document.querySelectorAll(".card");
let flippedCards = 0;
let flippedCardNumbers = [];
let flippedCardElements = [];
cardsContainer.innerHTML = "";
[...cards].sort(() => Math.random() - 0.5).forEach(card => cardsContainer.appendChild(card));
function flipCard(card) {
card.classList.add("flipped");
card.style.transform = "perspective(256px) rotateY(180deg)";
card.querySelector(".front-card").style.transform = "perspective(256px) rotateY(180deg)";
card.querySelector(".back-card").style.transform = "perspective(256px) rotateY(0deg)";
}
function unflipCard(card) {
card.classList.remove("flipped");
card.style.transform = "perspective(256px) rotateY(0deg)";
card.querySelector(".front-card").style.transform = "perspective(256px) rotateY(0deg)";
card.querySelector(".back-card").style.transform = "perspective(256px) rotateY(180deg)";
}
function checkMatch() {
if (flippedCardNumbers.length === 2) {
const [number1, number2] = flippedCardNumbers;
if (number1 === number2) {
setTimeout(() => {
flippedCardElements.forEach(card => card.classList.add('matched'));
flippedCardNumbers = [];
flippedCardElements = [];
flippedCards = 0;
}, 500);
} else {
setTimeout(() => {
flippedCardElements.forEach(card => unflipCard(card));
flippedCardNumbers = [];
flippedCardElements = [];
flippedCards = 0;
}, 1000);
}
}
}
cards.forEach(card => {
card.addEventListener("click", function() {
if (!card.classList.contains('flipped') && flippedCards < 2) {
flipCard(card);
flippedCards++;
flippedCardNumbers.push(card.querySelector(".back-card").textContent);
flippedCardElements.push(card);
checkMatch();
}
});
});
});