-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (92 loc) · 2.36 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
86
87
88
89
90
91
92
93
94
const container = document.querySelector('.container');
const game = {
cardsRight: '',
activeCard: [],
cardImages: [
'js',
'html',
'css',
'react',
'angular',
'vue',
'php',
'python',
'java',
'c',
'js',
'html',
'css',
'react',
'angular',
'vue',
'php',
'python',
'java',
'c',
],
indexArr: [],
randomizeCards() {
for (let i = 0; i < 20; i++) {
const sort = Math.floor(Math.random() * 20);
if (game.indexArr.includes(sort)) {
i--;
} else {
game.indexArr.push(sort);
}
}
},
createCards() {
game.randomizeCards();
for (i = 0; i < 20; i++) {
const card = document.createElement('div');
const cardRight = document.createElement('div');
const cardLeft = document.createElement('div');
card.classList.add('card');
cardRight.classList.add('card-right', 'opacity-one');
cardLeft.classList.add('card-left', 'opacity-zero');
cardLeft.style.backgroundImage = `url(./img/${
game.cardImages[game.indexArr[i]]
}.png)`;
container.appendChild(card);
card.append(cardRight, cardLeft);
}
this.cardsRight = document.querySelectorAll('.card-right');
this.cardsRight.forEach((card) => {
card.addEventListener('click', game.rotateCard);
});
},
rotateCard(e) {
if (e.target.nextSibling.classList.contains('active')) {
e.preventDefault();
} else {
if (game.activeCard.length < 2) {
e.target.classList.add('opacity-zero', 'rotate');
e.target.classList.remove('opacity-one');
e.target.nextSibling.classList.add('opacity-one', 'active', 'rotate');
e.target.nextSibling.classList.remove('opacity-zero');
game.activeCard.push(e.target.nextSibling.style.backgroundImage);
setTimeout(game.rotateBack, 2000);
}
}
},
rotateBack() {
const rotatedCards = document.querySelectorAll('.active');
if (
rotatedCards.length === 2 &&
game.activeCard[0] !== game.activeCard[1]
) {
rotatedCards.forEach((rotatedCard) => {
rotatedCard.classList.remove('opacity-one', 'active', 'rotate');
rotatedCard.previousSibling.classList.add('opacity-one');
rotatedCard.previousSibling.classList.remove('rotate');
});
game.activeCard = [];
} else if (game.activeCard[0] === game.activeCard[1]) {
rotatedCards.forEach((rotatedCard) => {
rotatedCard.remove();
});
game.activeCard = [];
}
},
};
document.addEventListener('DOMContentLoaded', game.createCards);