-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImggallery.js
65 lines (58 loc) · 2.48 KB
/
Imggallery.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
// Wait for the DOM to load completely before running the script
document.addEventListener('DOMContentLoaded', () => {
// Select all gallery item
const galleryItems = document.querySelectorAll('.gallery-item img');
const modal = document.getElementById('image-modal');
const modalImg = document.getElementById('modal-img');
const closeBtn = document.querySelector('.close');
const autoZoomBtn = document.getElementById('auto-zoom');
// Variables to manage auto-zoom functionality
let autoZoomIndex = 0;
let autoZoomInterval;
// Add click event listeners to each gallery item to open them in the modal
galleryItems.forEach(item => {
item.addEventListener('click', () => {
// Display the modal and set the modal image source to the clicked image
modal.style.display = 'block';
modalImg.src = item.src;
});
});
// Add click event listener to the close button to hide the modal and stop auto-zoom
closeBtn.addEventListener('click', () => {
modal.style.display = 'none';
clearInterval(autoZoomInterval);
});
// Add click event listener to window to close the modal if user clicks outside of the image
window.addEventListener('click', (event) => {
if (event.target == modal) {
modal.style.display = 'none';
clearInterval(autoZoomInterval);
}
});
// Add click event listener to the auto-zoom button
autoZoomBtn.addEventListener('click', () => {
autoZoomIndex = 0;
modal.style.display = 'block';
autoZoomImages();
autoZoomInterval = setInterval(autoZoomImages, 3000);
});
// Function to handle auto-zooming images
function autoZoomImages() {
if (autoZoomIndex < galleryItems.length) {
modalImg.src = galleryItems[autoZoomIndex].src;
// Apply zoom and brightness effects
modalImg.style.transform = 'scale(1.2)';
modalImg.style.filter = 'brightness(1.2)';
// After 2 seconds, reset the effects and move to the next image
setTimeout(() => {
modalImg.style.transform = 'scale(1)';
modalImg.style.filter = 'brightness(1)';
autoZoomIndex++;
// If the last image has been displayed, stop auto-zoom
if (autoZoomIndex >= galleryItems.length) {
clearInterval(autoZoomInterval);
}
}, 2000);
}
}
});