-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (51 loc) · 2.29 KB
/
script.js
File metadata and controls
52 lines (51 loc) · 2.29 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
dragElement(document.getElementById('plant1'));
dragElement(document.getElementById('plant2'));
dragElement(document.getElementById('plant3'));
dragElement(document.getElementById('plant4'));
dragElement(document.getElementById('plant5'));
dragElement(document.getElementById('plant6'));
dragElement(document.getElementById('plant7'));
dragElement(document.getElementById('plant8'));
dragElement(document.getElementById('plant9'));
dragElement(document.getElementById('plant10'));
dragElement(document.getElementById('plant11'));
dragElement(document.getElementById('plant12'));
dragElement(document.getElementById('plant13'));
dragElement(document.getElementById('plant14'));
function dragElement(terrariumElement) {
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
terrariumElement.onpointerdown = pointerDrag;
terrariumElement.ondblclick = function() {
const clone = terrariumElement.cloneNode(true); // clone the element
clone.id = terrariumElement.id + '-clone'; // give it a new id
const computedStyle = window.getComputedStyle(terrariumElement);
clone.style.position = 'absolute'; // set position to absolute
clone.style.top = terrariumElement.getBoundingClientRect().top + window.scrollY + 10 + 'px'; // offset slightly
clone.style.left = terrariumElement.getBoundingClientRect().left + window.scrollX + 10 + 'px'; // offset slightly
clone.style.width = computedStyle.width;
clone.style.height = computedStyle.height;
document.body.appendChild(clone); // append the clone to the body
dragElement(clone); // make the clone draggable
}
function pointerDrag(e) {
e.preventDefault();
console.log(e);
pos3 = e.clientX;
pos4 = e.clientY;
document.onpointermove = elementDrag;
document.onpointerup = stopElementDrag;
}
function elementDrag(e) {
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
console.log(pos1, pos2, pos3, pos4);
terrariumElement.style.top = terrariumElement.offsetTop - pos2 + 'px';
terrariumElement.style.left = terrariumElement.offsetLeft - pos1 + 'px';
}
function stopElementDrag() {
document.onpointerup = null;
document.onpointermove = null;
}
}