-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
37 lines (32 loc) · 979 Bytes
/
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
let rotateDeg = 0;
const card = document.getElementById("card");
const front = document.getElementById("front");
const back = document.getElementById("back");
const flipIcons = [front, back].map(el => el.querySelector(".flip"));
flipIcons.forEach(icon => icon.addEventListener("click", e => clickFlip(e.target)));
back.setAttribute("inert", "");
// Tease spin on initial load
setTimeout(() => setRotate(30), 1);
setTimeout(() => setRotate(0), 500);
function clickFlip(target) {
rotate180();
setFocusInert();
flipIcons.filter(i => i !== target)[0].focus();
}
function rotate180() {
setRotate(rotateDeg + 180);
}
function setRotate(deg) {
rotateDeg = deg;
card.style.transform = `rotateY(${rotateDeg}deg)`;
}
function setFocusInert() {
let focused = front;
let unfocused = back;
if (rotateDeg % 360) {
focused = back;
unfocused = front;
}
unfocused.setAttribute("inert", "");
focused.removeAttribute("inert");
}