-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
278 lines (231 loc) · 8.48 KB
/
Copy pathscript.js
File metadata and controls
278 lines (231 loc) · 8.48 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
function layoutImages(callback){
gallery = $('#gallery'); // cache a reference to our container
imagelist = $('#gallery img'); // cache a reference to our image list
var horizontalGap = 0.01 * screen.width;
var verticalGap = horizontalGap;
var containerWidth = gallery.width();
var columncount = 3; // or any other number of columns you want to display
if (screen.width <= 767) {
columncount = 1; // set number of columns to 1 for mobile devices
verticalGap = 15;
}
var fixedwidth = containerWidth / columncount + horizontalGap;
columns = [];
for (var i =0;i<columncount;i++){ // initialize columns (0 height for each)
columns.push(0);
}
imagelist.each(function(i,image){
if ($(image).is(':hidden')) // check if the image is hidden
{
$(image).addClass('bottom'); // add a class to the hidden image
$(image).css({top: gallery.height()}); // set the top property of the hidden image to the height of the container element
}
else
{
var min = Math.min.apply(null, columns), // find height of shortest column
index = columns.indexOf(min), // find column number with the min height
x = index*fixedwidth; // calculate horizontal position of current image based on column it belongs
x += (containerWidth - columncount * fixedwidth) / columncount; // offset to the left to recenter the images
columns[index] += image.height + verticalGap; //calculate new height of column
$(image).css({left:x, top:min}).delay(0).animate({},100, function() {
// call the callback function after the animation is complete
if (i === imagelist.length - 1) {
callback();
}
}); // assign new position to image and show it
}
});
}
function debounce(func, wait) {
var timeout;
return function() {
var context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
func.apply(context, args);
}, wait);
};
}
$(window).on('load', function(){
layoutImages(function() {
// hide the loading screen when the images have finished loading and the layout is complete
$('#loading-screen').hide();
});
$(window).resize(debounce(layoutImages, 100));
$(window).on('orientationchange', debounce(layoutImages, 100));
});
window.addEventListener("deviceorientation", function(event) {
layoutImages();
}, true);
// Get all the images on the page
var images = document.querySelectorAll("img");
// Loop through each image and add a click event listener
for (var i = 0; i < images.length; i++) {
images[i].addEventListener("click", function() {
// Find the index of the clicked image
var index = Array.prototype.indexOf.call(images, this);
// Create a new modal element
var modal = document.createElement("div");
modal.classList.add("modal");
// Create a new image element inside the modal
var modalImg = document.createElement("img");
modalImg.src = this.src.replace("/preview/", "/modal/");
modal.appendChild(modalImg);
if (screen.width >= 767) {
// Create left arrow button
var leftButton = document.createElement("div");
leftButton.classList.add("modal-arrow-button", "modal-arrow-left");
leftButton.innerHTML = "❮";
modal.appendChild(leftButton);
// Create right arrow button
var rightButton = document.createElement("div");
rightButton.classList.add("modal-arrow-button", "modal-arrow-right");
rightButton.innerHTML = "❯";
modal.appendChild(rightButton);
}
// Create close button
var closeButton = document.createElement("div");
closeButton.classList.add("modal-close-button");
closeButton.innerHTML = "✕";
modal.appendChild(closeButton);
// Add the modal to the page
document.body.appendChild(modal);
// Add a click event listener to the close button to close the modal
closeButton.addEventListener("click", function() {
closeModal();
});
if (screen.width >= 767) {
// Add a click event listener to the left arrow button to show the previous image
leftButton.addEventListener("click", function() {
var prevImage = images[index - 1];
if (prevImage) {
modalImg.src = prevImage.src.replace("/preview/", "/modal/");
index--;
}
});
// Add a click event listener to the right arrow button to show the next image
rightButton.addEventListener("click", function() {
var nextImage = images[index + 1];
if (nextImage) {
modalImg.src = nextImage.src.replace("/preview/", "/modal/");
index++;
}
});
}
// Add a keydown event listener to the document to close the modal when the Escape key is pressed
document.addEventListener("keydown", function(event) {
if (event.key === "Escape") {
closeModal();
} else if (event.key === "ArrowLeft") {
var prevImage = images[index - 1];
if (prevImage) {
modalImg.src = prevImage.src.replace("/preview/", "/modal/");
index--;
}
} else if (event.key === "ArrowRight") {
var nextImage = images[index + 1];
if (nextImage) {
modalImg.src = nextImage.src.replace("/preview/", "/modal/");
index++;
}
}
});
// Show the modal with an animation
setTimeout(function() {
modal.classList.add("show");
}, 10);
// Function to close the modal
function closeModal() {
modal.classList.remove("show");
setTimeout(function() {
modal.remove();
}, 300);
}
});
}
// Preload images
var previewImages = document.querySelectorAll("img");
var modalImageUrls = [];
for (var i = 0; i < previewImages.length; i++) {
var previewSrc = previewImages[i].getAttribute("src");
var modalSrc = previewSrc.replace("preview/", "modal/");
modalImageUrls.push(modalSrc);
}
const preloadImage = src =>
new Promise((resolve, reject) => {
const image = new Image()
image.onload = resolve
image.onerror = reject
image.src = src
})
for (var i = 0; i < modalImageUrls.length; i++) {
// preloadImage(modalImageUrls[i]);
}
const filterButtons = document.querySelectorAll('.filter-button');
const photos = document.querySelectorAll('#gallery img');
filterButtons.forEach(button => {
button.addEventListener('click', () => {
const filter = button.dataset.filter;
photos.forEach(photo => {
if (filter == 'all' || photo.classList.contains(filter)) {
photo.style.display = 'inline-block';
} else {
photo.style.display = 'none';
}
});
layoutImages();
});
});
if (false) {
if (screen.width <= 767) {
$(document).ready(function() {
var $window = $(window);
var $images = $('#gallery img');
var windowHeight = $window.height();
var windowWidth = $window.width();
function updateCarousel() {
var scrollTop = $window.scrollTop();
var center = scrollTop + windowHeight / 2;
$images.each(function() {
var $image = $(this);
var imageTop = $image.offset().top;
var imageHeight = $image.height();
var distanceTop = Math.abs(center - imageTop);
if (distanceTop < windowHeight) {
var distanceCentre = Math.abs(center - (imageTop + imageHeight / 2));
var distanceBottom = Math.abs(center - (imageTop + imageHeight));
if (center - (imageTop + imageHeight) < 0) {
var distance = Math.min(distanceTop, distanceCentre, distanceBottom);
var maxDistance = windowHeight * 1.5;
var scale = ((1.5 - (distance / maxDistance)) / 1.5) + 0.1;
var opacity = ((1.5 - (distance / maxDistance)) / 1.5) + 0.1;
if (scale < 0.8) {
scale = 0.8;
}
if (scale > 1.0) {
scale = 1.0;
}
if (opacity < 0.8) {
opacity = 0.8;
}
$image.css('transform', 'scale(' + scale + ')');
$image.css('opacity', opacity);
}
}
});
}
$window.on('scroll', updateCarousel);
$('.filter-button').on('click', function() {
var filter = $(this).data('filter');
$('#gallery img').hide();
if (filter === 'all') {
$('#gallery img').show();
} else {
$('#gallery img.' + filter).show();
}
updateCarousel();
});
});
}
}