Skip to content

Commit 93e3f94

Browse files
committed
updates
1 parent e3466ff commit 93e3f94

File tree

5 files changed

+48
-62
lines changed

5 files changed

+48
-62
lines changed

docs/04-js/src/script02.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// This is our JavaScript file. We'll add code here in the upcoming lessons.
2-
3-
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
4-
51
function dragElement(terrariumElement) {
6-
// Function body will be added in upcoming lessons.
7-
}
2+
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
3+
}

docs/04-js/src/script03.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
2-
31
function dragElement(terrariumElement) {
4-
// Function body will be added in upcoming lessons.
2+
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
53
}
64

7-
// Select plant elements
8-
document.getElementById('plant1');
9-
document.getElementById('plant2');
5+
dragElement(document.getElementById('plant1'));
6+
dragElement(document.getElementById('plant2'));

docs/04-js/src/script04.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
2-
31
function dragElement(terrariumElement) {
2+
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
43
terrariumElement.onpointerdown = pointerDrag;
4+
5+
function pointerDrag(e) {
6+
e.preventDefault();
7+
console.log(e);
8+
pos3 = e.clientX;
9+
pos4 = e.clientY;
10+
11+
12+
document.onpointermove = elementDrag;
13+
document.onpointerup = stopElementDrag;
14+
}
15+
16+
function elementDrag(e) {
17+
pos1 = pos3 - e.clientX;
18+
pos2 = pos4 - e.clientY;
19+
pos3 = e.clientX;
20+
pos4 = e.clientY;
21+
terrariumElement.style.top = (terrariumElement.offsetTop - pos2) + 'px';
22+
terrariumElement.style.left = (terrariumElement.offsetLeft - pos1) + 'px';
23+
}
24+
25+
function stopElementDrag() {
26+
document.onpointerup = null;
27+
document.onpointermove = null;
28+
}
529
}
630

7-
document.getElementById('plant1').onpointerdown = function() {
8-
dragElement(document.getElementById('plant1'));
9-
};
10-
document.getElementById('plant2').onpointerdown = function() {
11-
dragElement(document.getElementById('plant2'));
12-
};
31+
dragElement(document.getElementById('plant1'));
32+
dragElement(document.getElementById('plant2'));

docs/04-js/src/script05.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
2-
31
function dragElement(terrariumElement) {
42
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
53
terrariumElement.onpointerdown = pointerDrag;
64

75
function pointerDrag(e) {
86
e.preventDefault();
7+
console.log(e);
98
pos3 = e.clientX;
109
pos4 = e.clientY;
1110
document.onpointermove = elementDrag;
@@ -27,9 +26,17 @@ function dragElement(terrariumElement) {
2726
}
2827
}
2928

30-
document.getElementById('plant1').onpointerdown = function() {
31-
dragElement(document.getElementById('plant1'));
32-
};
33-
document.getElementById('plant2').onpointerdown = function() {
34-
dragElement(document.getElementById('plant2'));
35-
};
29+
dragElement(document.getElementById('plant1'));
30+
dragElement(document.getElementById('plant2'));
31+
dragElement(document.getElementById('plant3'));
32+
dragElement(document.getElementById('plant4'));
33+
dragElement(document.getElementById('plant5'));
34+
dragElement(document.getElementById('plant6'));
35+
dragElement(document.getElementById('plant7'));
36+
dragElement(document.getElementById('plant8'));
37+
dragElement(document.getElementById('plant9'));
38+
dragElement(document.getElementById('plant10'));
39+
dragElement(document.getElementById('plant11'));
40+
dragElement(document.getElementById('plant12'));
41+
dragElement(document.getElementById('plant13'));
42+
dragElement(document.getElementById('plant14'));

docs/04-js/src/script06.js

-34
This file was deleted.

0 commit comments

Comments
 (0)