Spaces:
Running
Running
File size: 7,116 Bytes
c3354c8 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7051847 4f9832c 7051847 4f9832c 7051847 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 7f8db79 4f9832c 84d8582 | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Gallery</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
.masonry-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1rem;
width: 100%;
}
.image-container {
aspect-ratio: 1;
position: relative;
overflow: hidden;
}
.gallery-image {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.modal {
display: none;
position: fixed;
inset: 0;
background-color: rgba(0, 0, 0, 0.9);
z-index: 50;
padding: 2rem;
cursor: pointer;
}
.modal-content {
max-height: 90vh;
max-width: 90vw;
margin: auto;
display: block;
object-fit: contain;
}
.toast {
position: fixed;
bottom: 1rem;
right: 1rem;
padding: 1rem;
border-radius: 0.5rem;
color: white;
animation: slideIn 0.3s ease-out;
z-index: 50;
}
.toast-success {
background-color: rgb(34, 197, 94);
}
.toast-error {
background-color: rgb(239, 68, 68);
}
@keyframes slideIn {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
</style>
</head>
<body>
<div class="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
<div class="space-y-8">
<div>
<h1 class="text-4xl font-bold tracking-tight text-gray-900">
Image Gallery
</h1>
<p class="mt-2 text-lg text-gray-600">
Upload your images by dragging and dropping them below
</p>
</div>
<div id="dropZone" class="relative min-h-[200px] rounded-xl border-2 border-dashed border-gray-200 hover:border-purple-500/50 transition-all duration-300 ease-in-out cursor-pointer">
<input type="file" id="fileInput" class="hidden" multiple accept="image/*"/>
<div class="absolute inset-0 flex flex-col items-center justify-center space-y-4 p-6 text-center">
<div class="rounded-full bg-purple-500/10 p-4">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="rgb(147, 51, 234)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
<polyline points="17 8 12 3 7 8"></polyline>
<line x1="12" y1="3" x2="12" y2="15"></line>
</svg>
</div>
<div>
<p class="text-lg font-medium text-gray-900">
Drag & Drop images here
</p>
<p class="mt-1 text-sm text-gray-500">or click anywhere to upload</p>
</div>
</div>
</div>
<div class="space-y-4">
<h2 class="text-2xl font-semibold text-gray-900">
Your Images
</h2>
<div id="gallery" class="masonry-grid">
<!-- Images will be dynamically added here -->
</div>
<p id="noImages" class="text-center text-gray-500 py-12">
No images uploaded yet. Start by dropping some images above!
</p>
</div>
</div>
</div>
<!-- Modal for full-size image view -->
<div id="imageModal" class="modal" onclick="closeModal()">
<img id="modalImage" class="modal-content" src="" alt="Full size image">
</div>
<script>
// Initialize variables
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const gallery = document.getElementById('gallery');
const noImages = document.getElementById('noImages');
const modal = document.getElementById('imageModal');
const modalImg = document.getElementById('modalImage');
let images = [];
// Toast notification function
function showToast(message, type = 'success') {
const toast = document.createElement('div');
toast.className = `toast toast-${type}`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 3000);
}
// Modal functions
function openModal(imageSrc) {
modal.style.display = 'flex';
modalImg.src = imageSrc;
}
function closeModal() {
modal.style.display = 'none';
}
// Setup gallery grid
function setupGalleryGrid() {
gallery.innerHTML = '';
images.forEach((image, index) => {
const imgContainer = document.createElement('div');
imgContainer.className = 'image-container rounded-lg overflow-hidden bg-gray-100 shadow-sm hover:shadow-md transition-all duration-300 cursor-pointer';
const img = document.createElement('img');
img.src = image;
img.alt = `Gallery image ${index + 1}`;
img.className = 'gallery-image';
img.loading = 'lazy';
imgContainer.onclick = () => openModal(image);
imgContainer.appendChild(img);
gallery.appendChild(imgContainer);
});
// Toggle no images message
noImages.style.display = images.length > 0 ? 'none' : 'block';
}
// Handle file addition
function handleFiles(files) {
const imageFiles = Array.from(files).filter(file => file.type.startsWith('image/'));
if (imageFiles.length === 0) {
showToast('Please select image files only', 'error');
return;
}
const newImages = imageFiles.map(file => URL.createObjectURL(file));
images = [...images, ...newImages];
setupGalleryGrid();
showToast(`${imageFiles.length} image${imageFiles.length === 1 ? '' : 's'} uploaded`);
}
// Event Listeners
dropZone.addEventListener('dragenter', (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.add('border-purple-500', 'bg-purple-500/5');
});
dropZone.addEventListener('dragleave', (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.remove('border-purple-500', 'bg-purple-500/5');
});
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
dropZone.classList.remove('border-purple-500', 'bg-purple-500/5');
handleFiles(e.dataTransfer.files);
});
dropZone.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', (e) => {
handleFiles(e.target.files);
e.target.value = '';
});
// Close modal when pressing escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
closeModal();
}
});
// Initial setup
setupGalleryGrid();
</script>
</body>
</html> |