-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
85 lines (67 loc) · 1.79 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var cards = document.querySelectorAll('.card');
var hasFlippedCard = false;
var lockboard = false;
var firstCard, secondCard;
var firstImage, secondImage;
function flipCard() {
if (lockboard) return;
//handle double clicking bug
if (this === firstCard) return;
this.classList.add('flip');
//get the front face image and show it, add a little bit timeout for effect
let cardImage = this.querySelector(".front-face")
setTimeout(function () {
cardImage.classList.add('d-block');
cardImage.classList.remove("d-none")
}, 100)
if (!hasFlippedCard) {
//first click
hasFlippedCard = true;
firstCard = this;
firstImage = cardImage;
return;
}
//second click
hasFlippedCard = false;
secondCard = this;
secondImage = cardImage;
checkMatch();
}
cards.forEach(card => card.addEventListener('click', flipCard));
console.log(cards);
function checkMatch() {
if (firstCard.dataset.album === secondCard.dataset.album) {
disableCards();
} else {
unflipCards();
}
}
function disableCards() {
firstCard.removeEventListener('click', flipCard);
secondCard.removeEventListener('click', flipCard);
resetBoard();
}
function unflipCards() {
lockboard = true;
setTimeout(() => {
firstCard.classList.remove('flip');
secondCard.classList.remove('flip');
//reset images
firstImage.classList.add('d-none');
firstImage.classList.remove("d-block")
secondImage.classList.add('d-none');
secondImage.classList.remove("d-block")
resetBoard();
}, 1000);
}
function resetBoard() {
[hasFlippedCard, lockboard] = [false, false];
[firstCard, secondCard] = [null, null];
}
//shuffle cards IIFE
(function shuffle() {
cards.forEach(card => {
var randomPos = Math.floor(Math.random() * 18);
card.style.order = randomPos;
});
})();