|
7 | 7 | var captionText = $('#caption'); |
8 | 8 | var closeBtn = $('.close'); |
9 | 9 |
|
| 10 | + // Gallery state |
| 11 | + var currentImageIndex = 0; |
| 12 | + var imageElements = []; |
| 13 | + |
10 | 14 | // Transform state |
11 | 15 | var scale = 1; |
12 | 16 | var translateX = 0; |
|
30 | 34 | updateTransform(); |
31 | 35 | } |
32 | 36 |
|
| 37 | + function showImage(index) { |
| 38 | + if (index < 0 || index >= imageElements.length) return; |
| 39 | + |
| 40 | + currentImageIndex = index; |
| 41 | + var img = imageElements[index]; |
| 42 | + var fullSrc = $(img).data('full') || img.src; |
| 43 | + |
| 44 | + modalImg.attr('src', fullSrc); |
| 45 | + captionText.text($(img).attr('alt') + ` (${index + 1} of ${imageElements.length})`); |
| 46 | + resetTransform(); |
| 47 | + |
| 48 | + // Update navigation button states |
| 49 | + updateNavigationButtons(); |
| 50 | + } |
| 51 | + |
| 52 | + function updateNavigationButtons() { |
| 53 | + $('.modal-prev').toggleClass('disabled', currentImageIndex === 0); |
| 54 | + $('.modal-next').toggleClass('disabled', currentImageIndex === imageElements.length - 1); |
| 55 | + } |
| 56 | + |
| 57 | + function goToPrevious() { |
| 58 | + if (currentImageIndex > 0) { |
| 59 | + showImage(currentImageIndex - 1); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + function goToNext() { |
| 64 | + if (currentImageIndex < imageElements.length - 1) { |
| 65 | + showImage(currentImageIndex + 1); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Initialize gallery - collect all images on page load |
| 70 | + function initializeGallery() { |
| 71 | + imageElements = $('.image.fit img, .image.main img').get(); |
| 72 | + } |
| 73 | + |
33 | 74 | // Add click event to all images |
34 | | - $('.image.fit img, .image.main img').on('click', function() { |
| 75 | + $(document).on('click', '.image.fit img, .image.main img', function() { |
| 76 | + initializeGallery(); |
| 77 | + currentImageIndex = imageElements.indexOf(this); |
35 | 78 | modal.show(); |
36 | | - modalImg.attr('src', this.src); |
37 | | - captionText.text($(this).attr('alt')); |
38 | | - resetTransform(); |
| 79 | + showImage(currentImageIndex); |
| 80 | + $('body').addClass('modal-open'); |
| 81 | + }); |
| 82 | + |
| 83 | + // Keyboard navigation |
| 84 | + $(document).on('keydown', function(e) { |
| 85 | + if (!modal.is(':visible')) return; |
| 86 | + |
| 87 | + switch(e.key) { |
| 88 | + case 'Escape': |
| 89 | + modal.hide(); |
| 90 | + $('body').removeClass('modal-open'); |
| 91 | + break; |
| 92 | + case 'ArrowLeft': |
| 93 | + e.preventDefault(); |
| 94 | + goToPrevious(); |
| 95 | + break; |
| 96 | + case 'ArrowRight': |
| 97 | + e.preventDefault(); |
| 98 | + goToNext(); |
| 99 | + break; |
| 100 | + } |
39 | 101 | }); |
40 | 102 |
|
| 103 | + // Navigation button events |
| 104 | + $(document).on('click', '.modal-prev:not(.disabled)', goToPrevious); |
| 105 | + $(document).on('click', '.modal-next:not(.disabled)', goToNext); |
| 106 | + |
41 | 107 | // Mouse wheel zoom |
42 | 108 | modalImg.on('wheel', function(e) { |
43 | 109 | e.preventDefault(); |
|
132 | 198 | // Close modal events |
133 | 199 | closeBtn.on('click', function() { |
134 | 200 | modal.hide(); |
| 201 | + $('body').removeClass('modal-open'); |
135 | 202 | }); |
136 | 203 |
|
137 | 204 | modal.on('click', function(event) { |
138 | 205 | if (event.target === this) { |
139 | 206 | modal.hide(); |
140 | | - } |
141 | | - }); |
142 | | - |
143 | | - $(document).on('keydown', function(e) { |
144 | | - if (e.key === 'Escape') { |
145 | | - modal.hide(); |
| 207 | + $('body').removeClass('modal-open'); |
146 | 208 | } |
147 | 209 | }); |
148 | 210 | }); |
|
0 commit comments