forked from HackYourFuture-CPH/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove-element.js
More file actions
65 lines (51 loc) · 2.18 KB
/
move-element.js
File metadata and controls
65 lines (51 loc) · 2.18 KB
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
// Creating new scope
{
const redBox = document.querySelector('ul.marks li:nth-child(1)');
const blueBox = document.querySelector('ul.marks li:nth-child(2)');
const greenBox = document.querySelector('ul.marks li:nth-child(3)');
const boxes = [redBox, blueBox, greenBox];
const span = document.querySelector('span');
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomizeStartPosition() {
boxes.forEach(box => {
const x = getRandomInt(-20, 20);
const y = getRandomInt(-20, 20);
box.style.left = x;
box.style.top = y;
});
}
randomizeStartPosition();
const targets = document.querySelectorAll('ul.targets li');
// continously check if circles has been added to the right targets
setInterval(() => {
setTargetFulfilled(redBox, targets[0], {x: 20, y: 300});
setTargetFulfilled(blueBox, targets[1], {x: 400, y: 300});
setTargetFulfilled(greenBox, targets[2], {x: 400, y: 20});
const allTargetsFulfilled = [...targets].every(target => target.classList.contains('fulfilled'));
if (allTargetsFulfilled) {
setTimeout(() => span.classList.add('shown'), 300);
}
}, 10);
function setTargetFulfilled(box, targetBox, fulfilledPosition) {
const renderedClientBox = box.getBoundingClientRect();
if (renderedClientBox.left === fulfilledPosition.x && renderedClientBox.top === fulfilledPosition.y) {
targetBox.classList.add('fulfilled');
}
}
/**
* @param {DOMelement} boxToMove - A DOM element of the box to move
* @param {Position} newPosition - An object specifying the new position of the box. x, y
* @return {Promise<any>}
*/
function moveElement(boxToMove, newPosition) {
return new Promise(resolve => {
boxToMove.style.transform = `translate(${newPosition.x}px, ${newPosition.y}px)`;
boxToMove.addEventListener('transitionend', resolve);
});
}
window.moveElement = moveElement;
}