Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const uploadBtn = document.getElementById('uploadBtn');
+ const fileInput = document.getElementById('fileInput');
+ const gallery = document.getElementById('photoGallery');
+ const modal = document.getElementById('photoModal');
+ const modalImage = document.getElementById('modalImage');
+ const downloadBtn = document.getElementById('downloadBtn');
+ const closeModal = document.getElementById('closeModal');
+
+ // Upload functionality
+ uploadBtn.addEventListener('click', () => fileInput.click());
+
+ fileInput.addEventListener('change', (e) => {
+ const files = Array.from(e.target.files);
+
+ files.forEach(file => {
+ if (file.type.startsWith('image/')) {
+ const reader = new FileReader();
+
+ reader.onload = (e) => {
+ const photoItem = document.createElement('div');
+ photoItem.className = 'photo-item';
+
+ const img = document.createElement('img');
+ img.src = e.target.result;
+ img.alt = file.name;
+
+ photoItem.appendChild(img);
+ gallery.appendChild(photoItem);
+
+ // Add click event for modal
+ photoItem.addEventListener('click', () => {
+ modalImage.src = e.target.result;
+ modalImage.alt = file.name;
+ modal.showModal();
+ });
+ };
+
+ reader.readAsDataURL(file);
+ }
+ });
+ });
+
+ // Modal functionality
+ closeModal.addEventListener('click', () => {
+ modal.close();
+ });
+
+ downloadBtn.addEventListener('click', () => {
+ const link = document.createElement('a');
+ link.href = modalImage.src;
+ link.download = 'photo.jpg';
+ link.click();
+ });
+
+ // Close modal on backdrop click
+ modal.addEventListener('click', (e) => {
+ if (e.target === modal) {
+ modal.close();
+ }
+ });
+
+ // Keyboard accessibility
+ document.addEventListener('keydown', (e) => {
+ if (e.key === 'Escape' && modal.open) {
+ modal.close();
+ }
+ });
+ });