-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment03.js
More file actions
162 lines (134 loc) · 4.84 KB
/
Copy pathAssignment03.js
File metadata and controls
162 lines (134 loc) · 4.84 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
COMP125 Assignment #3 - DOM Slideshow
Student: Yushan Wang (301508247)
*/
let lightboxTitle = "My Western Vacation";
let imgFiles = [
"photo01.jpg","photo02.jpg","photo03.jpg","photo04.jpg",
"photo05.jpg","photo06.jpg","photo07.jpg","photo08.jpg",
"photo09.jpg","photo10.jpg","photo11.jpg","photo12.jpg"
];
let imgCaptions = [
"Sky Pond (Rocky Mountain National Park)",
"Buffalo on the Plains (South Dakota)",
"Garden of the Gods (Colorado Springs)",
"Elephant Head Wild Flower (Rocky Mountain National Park)",
"Double Rainbow (Colorado National Monument)",
"Moose in the Wild (Grand Lake, Colorado)",
"Camas Wild Flower (Rocky Mountain National Park)",
"Chasm Lake (Rocky Mountain National Park)",
"Teton Crest Trail (Grand Teton National Park)",
"The Notch Trail (Badlands National Park)",
"Sprague Lake (Rocky Mountain National Park)",
"Longs Peak Trail (Rocky Mountain National Park)"
];
let imgCount = imgFiles.length;
let currentImage = 1;
let timeID = null;
window.addEventListener("load", createLightbox);
function createLightbox() {
let lightBox = document.getElementById("lightBox");
// title
let lbTitle = document.createElement("h1");
lbTitle.id = "lbTitle";
lbTitle.textContent = lightboxTitle;
lightBox.appendChild(lbTitle);
// counter
let lbCounter = document.createElement("div");
lbCounter.id = "lbCounter";
lbCounter.textContent = `${currentImage} / ${imgCount}`;
lightBox.appendChild(lbCounter);
// controls
let lbPrev = document.createElement("button");
lbPrev.id = "lbPrev";
lbPrev.innerHTML = "◀";
lbPrev.classList.add("navBtn");
lbPrev.addEventListener("click", showPrev);
let lbNext = document.createElement("button");
lbNext.id = "lbNext";
lbNext.innerHTML = "▶";
lbNext.classList.add("navBtn");
lbNext.addEventListener("click", showNext);
let lbPlay = document.createElement("button");
lbPlay.id = "playPauseBtn";
lbPlay.textContent = "▶ Play";
lbPlay.addEventListener("click", togglePlay);
// image container
let lbImages = document.createElement("div");
lbImages.id = "lbImages";
lightBox.append(lbPrev, lbNext, lbImages, lbPlay);
// load images
imgFiles.forEach((src, i) => {
let image = document.createElement("img");
image.src = src;
image.alt = imgCaptions[i];
image.addEventListener("click", openOverlay);
lbImages.appendChild(image);
});
function showNext() {
lbImages.appendChild(lbImages.firstElementChild);
currentImage = currentImage < imgCount ? currentImage + 1 : 1;
lbCounter.textContent = `${currentImage} / ${imgCount}`;
}
function showPrev() {
lbImages.insertBefore(lbImages.lastElementChild, lbImages.firstElementChild);
currentImage = currentImage > 1 ? currentImage - 1 : imgCount;
lbCounter.textContent = `${currentImage} / ${imgCount}`;
}
function togglePlay() {
if (timeID) {
clearInterval(timeID);
timeID = null;
lbPlay.textContent = "▶ Play";
} else {
showNext();
timeID = setInterval(showNext, 2000);
lbPlay.textContent = "⏸ Pause";
}
}
}
// overlay (full-screen view)
function openOverlay() {
let overlay = document.createElement("div");
overlay.id = "overlay";
overlay.style.display = "flex";
let figure = document.createElement("figure");
let bigImg = document.createElement("img");
bigImg.src = this.src;
bigImg.alt = this.alt;
let caption = document.createElement("figcaption");
caption.textContent = this.alt;
let addBtn = document.createElement("button");
addBtn.textContent = "Add to Favorites";
addBtn.onclick = () => addFavorite(this.src);
let closeBtn = document.createElement("button");
closeBtn.id = "closeOverlay";
closeBtn.textContent = "Close";
closeBtn.onclick = () => document.body.removeChild(overlay);
figure.append(bigImg, caption, addBtn, closeBtn);
overlay.appendChild(figure);
document.body.appendChild(overlay);
}
// favorites logic
function addFavorite(src) {
let favList = document.getElementById("favList");
let message = document.getElementById("message");
if (favList.children.length >= 5) {
message.textContent = "You can only have up to 5 favorites. Remove one first!";
message.className = "error";
message.style.display = "block";
return;
}
let item = document.createElement("div");
item.classList.add("favItem");
let img = document.createElement("img");
img.src = src;
let removeBtn = document.createElement("button");
removeBtn.textContent = "Remove";
removeBtn.onclick = () => item.remove();
item.append(img, removeBtn);
favList.appendChild(item);
message.textContent = "Added to favorites!";
message.className = "success";
message.style.display = "block";
}