-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
70 lines (63 loc) · 1.75 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
let body = document.querySelector("body");
let input = document.getElementById("inpt-url");
let add = document.getElementById("add-btn");
let image = document.getElementById("image");
let documents = document.getElementById("documents");
documents.innerText =
"Please enter the Image URL and click on add button , then press 'P' for previous image, 'N' for next image, 'C' for clear current image, 'D' for delete all images.";
image.setAttribute("width", "300px");
image.setAttribute("height", "300px");
let arrImg = [];
let index = -1;
add.addEventListener("click", () => {
console.log("Add button clicked");
let url = input.value;
console.log(url);
image.setAttribute("src", url);
index++;
arrImg[index] = url;
console.log(arrImg);
});
body.addEventListener("keypress", (event) => {
console.log(event.key);
if (event.key === "p" || event.key === "P") {
gotoPrevious();
}
if (event.key === "n" || event.key === "N") {
gotoNext();
}
if (event.key === "c" || event.key === "C") {
clearImage();
}
if (event.key === "d" || event.key === "D") {
deleteAllImage();
}
});
function gotoPrevious() {
if (index > 0) {
index--;
image.setAttribute("src", arrImg[index]);
} else {
index = arrImg.length - 1;
image.setAttribute("src", arrImg[index]);
}
}
function gotoNext() {
if (index < arrImg.length - 1) {
index++;
image.setAttribute("src", arrImg[index]);
} else {
index = 0;
image.setAttribute("src", arrImg[index]);
}
}
function clearImage() {
arrImg.splice(index, 1);
index--;
image.setAttribute("src", arrImg[index]);
}
function deleteAllImage() {
arrImg.length = 0;
index = -1;
image.setAttribute("src", "");
}