-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
90 lines (76 loc) · 2.9 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
const dialog = document.querySelector('#dialog');
const galleryContainer = document.querySelector('.imageGallery');
const carouselContainer = document.querySelector('#carousel');
const closeDialogBtn = document.querySelector('#closeDialogBtn');
const nextButton = document.querySelector('.controls-btn.next');
const prevButton = document.querySelector('.controls-btn.prev');
// Array of image URLs
const images = [
"img/CLELFJhu.webp", "img/FcFjBvHx.webp", "img/TeAeZpxD.webp", "img/tKuctSHr.webp",
"img/akbFivud.webp", "img/mMhbtqvY.webp", "img/tBKkxMmk.webp", "img/tAndIWYz.webp"
];
let currentIndex = 0; // Track the current index of the displayed image
// Dynamically generate gallery buttons
images.forEach((image, index) => {
const button = document.createElement('button');
button.onclick = () => openDialog(index);
const img = document.createElement('img');
img.src = image;
img.alt = "Image";
img.loading = "lazy";
button.appendChild(img);
galleryContainer.appendChild(button);
});
// Dynamically generate carousel images
images.forEach((image) => {
const img = document.createElement('img');
img.src = image;
img.alt = "Image";
img.loading = "lazy";
carouselContainer.appendChild(img);
});
function openDialog(index) {
currentIndex = index;
dialog.showModal();
const image = carouselContainer.querySelectorAll('img')[currentIndex];
image.scrollIntoView({ behavior: 'smooth', inline: 'center' });
// Update the button states when opening the dialog
updateButtonStates();
}
function scrollCarousel(direction) {
// Update the currentIndex based on the direction
if (direction === 1 && currentIndex < images.length - 1) {
currentIndex++; // Move to next image
} else if (direction === -1 && currentIndex > 0) {
currentIndex--; // Move to previous image
}
// Scroll to the new image
const image = carouselContainer.querySelectorAll('img')[currentIndex];
image.scrollIntoView({ behavior: 'smooth', inline: 'center' });
// Update the button states after scrolling
updateButtonStates();
}
// Update button states (disable/enable next and previous)
function updateButtonStates() {
if (currentIndex === 0) {
prevButton.disabled = true; // Disable "Previous" button on the first image
} else {
prevButton.disabled = false;
}
if (currentIndex === images.length - 1) {
nextButton.disabled = true; // Disable "Next" button on the last image
} else {
nextButton.disabled = false;
}
}
// Event listeners for the buttons
nextButton.addEventListener('click', () => scrollCarousel(1));
prevButton.addEventListener('click', () => scrollCarousel(-1));
// Event listener for closing the dialog
closeDialogBtn.addEventListener('click', () => {
dialog.close();
// Ensure that the buttons reset when closing the dialog
updateButtonStates();
});
// Initial state check for the buttons
updateButtonStates(); // Call it on page load to set the correct button states