-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
231 lines (194 loc) · 7.07 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const imageContainer = document.getElementById('gallery');
let index = 0;
let loading = false;
const batchSize = 10; // The number of images loaded per batch
const initialBatchCount = 5; // The number of batches initially loaded
let currentImageIndex = 0;
let loadedImages = [];
let leftArrow;
let rightArrow;
// Calculate the number of days since the start date
function calculateLoveDays() {
const startDate = new Date('2020-01-01'); // **Love date**
const today = new Date();
startDate.setHours(0, 0, 0, 0);
today.setHours(0, 0, 0, 0);
const timeDiff = today - startDate;
const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
document.getElementById('loveDays').innerText = days;
}
// Load images in batches when the user scrolls to the bottom of the page
async function loadImages(batchCount = 1) {
if (loading) return;
loading = true;
for (let b = 0; b < batchCount; b++) {
const batchPromises = [];
for (let i = 0; i < batchSize; i++) {
batchPromises.push(loadThumbnail(index));
index++;
}
const results = await Promise.all(batchPromises);
results.forEach((img) => {
if (img) imageContainer.appendChild(img);
});
const loadMore = results.some((img) => img);
if (!loadMore) {
window.removeEventListener('scroll', handleScroll);
console.log('All images have been loaded and displayed.');
break;
}
}
loading = false;
}
// Load a thumbnail image and create an image element
function loadThumbnail(index) {
return new Promise((resolve) => {
const thumbImg = new Image();
thumbImg.crossOrigin = 'Anonymous';
thumbImg.src = `images/thumbs/${index}.jpg`;
thumbImg.onload = function () {
createImageElement(thumbImg, index, resolve);
};
// If the thumbnail image fails to load, try loading the full-size image
thumbImg.onerror = function () {
thumbImg.src = `images/${index}.jpg`;
thumbImg.onload = function () {
createImageElement(thumbImg, index, resolve);
};
thumbImg.onerror = function () {
resolve(null);
};
};
function createImageElement(thumbImg, index, resolve) {
const imgElement = document.createElement('img');
imgElement.dataset.large = `images/${index}.jpg`;
imgElement.src = thumbImg.src;
imgElement.alt = `Image ${index}`;
imgElement.setAttribute('data-date', '');
imgElement.setAttribute('data-index', index);
EXIF.getData(thumbImg, function () {
let exifDate = EXIF.getTag(this, 'DateTimeOriginal');
if (exifDate) {
exifDate = exifDate.replace(/^(\d{4}):(\d{2}):(\d{2}).*$/, '$1.$2.$3');
} else {
exifDate = 'Unknown date';
}
imgElement.setAttribute('data-date', exifDate);
loadedImages[index] = {
src: imgElement.dataset.large,
date: exifDate,
};
});
imgElement.addEventListener('click', function () {
showPopup(imgElement.dataset.large, imgElement.getAttribute('data-date'), index);
});
imgElement.style.cursor = 'pointer';
imgElement.classList.add('thumbnail');
resolve(imgElement);
}
});
}
// Display a popup with the full-size image
function showPopup(src, date, index) {
currentImageIndex = index;
const popup = document.getElementById('popup');
const popupImg = document.getElementById('popupImg');
const imgDate = document.getElementById('imgDate');
popup.style.display = 'block';
popupImg.style.display = 'none';
imgDate.innerText = '';
const fullImg = new Image();
fullImg.crossOrigin = 'Anonymous';
fullImg.src = src;
fullImg.onload = function () {
popupImg.src = src;
popupImg.style.display = 'block';
imgDate.innerText = date;
};
fullImg.onerror = function () {
imgDate.innerText = 'Load failed';
};
leftArrow.style.display = 'flex';
rightArrow.style.display = 'flex';
if (currentImageIndex > 0) {
leftArrow.classList.remove('disabled');
} else {
leftArrow.classList.add('disabled');
}
if (loadedImages[currentImageIndex + 1]) {
rightArrow.classList.remove('disabled');
} else {
rightArrow.classList.add('disabled');
}
}
// Close the popup
function closePopup() {
const popup = document.getElementById('popup');
const popupImg = document.getElementById('popupImg');
const imgDate = document.getElementById('imgDate');
popup.style.display = 'none';
popupImg.src = '';
imgDate.innerText = '';
leftArrow.style.display = 'none';
rightArrow.style.display = 'none';
}
// Load the previous image
function handleScroll() {
const scrollTop = window.scrollY;
const windowHeight = window.innerHeight;
const documentHeight = document.documentElement.scrollHeight;
if (scrollTop + windowHeight >= documentHeight - 1000) {
loadImages();
}
}
// Show the previous image in the popup (when the left arrow is clicked)
function showPreviousImage() {
const prevIndex = currentImageIndex - 1;
if (prevIndex >= 0) {
if (loadedImages[prevIndex]) {
currentImageIndex = prevIndex;
const imgData = loadedImages[prevIndex];
showPopup(imgData.src, imgData.date, prevIndex);
} else {
leftArrow.classList.add('disabled');
}
}
}
// Show the next image in the popup (when the right arrow is clicked)
function showNextImage() {
const nextIndex = currentImageIndex + 1;
if (loadedImages[nextIndex]) {
currentImageIndex = nextIndex;
const imgData = loadedImages[nextIndex];
showPopup(imgData.src, imgData.date, nextIndex);
} else {
rightArrow.classList.add('disabled');
}
}
// Keyboard navigation for the popup
window.addEventListener('keydown', function (event) {
const popup = document.getElementById('popup');
if (popup.style.display === 'block') {
if (event.key === 'ArrowLeft') {
showPreviousImage();
} else if (event.key === 'ArrowRight') {
showNextImage();
} else if (event.key === 'Escape') {
closePopup();
}
}
});
// Load the initial images and set up event listeners
window.onload = function () {
calculateLoveDays();
loadImages(initialBatchCount).then(() => {
window.addEventListener('scroll', handleScroll);
});
document.getElementById('closeBtn').addEventListener('click', closePopup);
leftArrow = document.getElementById('leftArrow');
rightArrow = document.getElementById('rightArrow');
leftArrow.addEventListener('click', showPreviousImage);
rightArrow.addEventListener('click', showNextImage);
leftArrow.style.display = 'none';
rightArrow.style.display = 'none';
};