Static-003 / index.html
pakdeveloper's picture
Update index.html
c5ec00d verified
Raw History Blame
11.7 kB
<!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>
<style>
/* CSS remains unchanged */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
min-height: 100vh;
background: #FAFAFA;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
}
h1 {
font-size: 1.875rem;
font-weight: bold;
color: #262626;
margin-bottom: 2rem;
}
.upload-zone {
background: white;
border: 2px dashed #e2e8f0;
border-radius: 0.5rem;
padding: 2rem;
margin-bottom: 2rem;
cursor: pointer;
transition: all 0.2s;
}
.upload-zone:hover,
.upload-zone.dragging {
border-color: #405DE6;
background: #f8fafc;
}
.upload-zone-content {
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
}
.upload-icon {
width: 3rem;
height: 3rem;
color: #405DE6;
}
.upload-text {
font-size: 1.125rem;
font-weight: 600;
}
.upload-subtext {
font-size: 0.875rem;
color: #64748b;
}
.select-button {
margin-top: 1rem;
padding: 0.5rem 1rem;
background: white;
border: 1px solid #e2e8f0;
border-radius: 0.375rem;
font-size: 0.875rem;
cursor: pointer;
transition: all 0.2s;
}
.select-button:hover {
background: #405DE6;
color: white;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1rem;
}
.image-card {
position: relative;
background: white;
border-radius: 0.5rem;
overflow: hidden;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.image-card img {
width: 100%;
aspect-ratio: 1;
object-fit: cover;
cursor: pointer;
}
.image-overlay {
position: absolute;
inset: 0;
background: rgba(0,0,0,0.4);
opacity: 0;
transition: opacity 0.2s;
display: flex;
align-items: center;
justify-content: center;
}
.image-card:hover .image-overlay {
opacity: 1;
}
.delete-button {
background: white;
border: none;
border-radius: 9999px;
width: 2.5rem;
height: 2.5rem;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s;
}
.delete-button:hover {
background: #ef4444;
color: white;
}
.modal {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.9);
display: none;
align-items: center;
justify-content: center;
z-index: 50;
padding: 20px;
}
.modal.active {
display: flex;
}
.modal img {
max-width: 95%;
max-height: 95vh;
object-fit: contain;
}
.toast {
position: fixed;
bottom: 1rem;
right: 1rem;
padding: 1rem;
background: #262626;
color: white;
border-radius: 0.375rem;
display: none;
animation: slideIn 0.2s ease-out;
}
.url-input-container {
margin: 20px 0;
display: flex;
gap: 10px;
width: 100%;
max-width: 600px;
}
.url-input {
flex: 1;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
}
.url-button {
padding: 8px 16px;
background: #405DE6;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.url-button:hover {
background: #304BD6;
}
.url-button:disabled {
background: #ccc;
cursor: not-allowed;
}
@keyframes slideIn {
from {
transform: translateY(100%);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Image Gallery</h1>
<div class="upload-zone" id="uploadZone">
<div class="upload-zone-content">
<svg class="upload-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"/>
</svg>
<div class="upload-text">Click or drag and drop your images here</div>
<div class="upload-subtext">Supports: JPG, PNG, GIF</div>
<button class="select-button">Select Image</button>
</div>
<input type="file" id="fileInput" accept="image/*" multiple hidden>
</div>
<div class="url-input-container">
<input type="text" id="urlInput" placeholder="Or paste image URL here" class="url-input">
<button class="url-button" onclick="addImageFromUrl()">Add URL</button>
</div>
<div class="gallery" id="gallery"></div>
</div>
<div class="modal" id="modal">
<img id="modalImage" src="" alt="Full size image">
</div>
<div class="toast" id="toast"></div>
<script>
const uploadZone = document.getElementById('uploadZone');
const fileInput = document.getElementById('fileInput');
const gallery = document.getElementById('gallery');
const modal = document.getElementById('modal');
const modalImage = document.getElementById('modalImage');
const toast = document.getElementById('toast');
let images = [];
function showToast(message) {
toast.textContent = message;
toast.style.display = 'block';
setTimeout(() => {
toast.style.display = 'none';
}, 3000);
}
function handleDrag(e) {
e.preventDefault();
e.stopPropagation();
if (e.type === "dragenter" || e.type === "dragover") {
uploadZone.classList.add('dragging');
} else if (e.type === "dragleave" || e.type === "drop") {
uploadZone.classList.remove('dragging');
}
}
uploadZone.addEventListener('click', () => fileInput.click());
uploadZone.addEventListener('dragenter', handleDrag);
uploadZone.addEventListener('dragover', handleDrag);
uploadZone.addEventListener('dragleave', handleDrag);
uploadZone.addEventListener('drop', (e) => {
handleDrag(e);
const files = Array.from(e.dataTransfer.files);
handleFiles(files);
});
fileInput.addEventListener('change', (e) => {
const files = Array.from(e.target.files || []);
handleFiles(files);
fileInput.value = '';
});
function handleFiles(files) {
files.forEach(file => {
if (!file.type.startsWith('image/')) {
showToast('Unsupported file format');
return;
}
if (file.size > 5 * 1024 * 1024) {
showToast('File size exceeds 5MB limit');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
addImage({
id: Date.now(),
url: e.target.result
});
showToast('Image uploaded successfully');
};
reader.readAsDataURL(file);
});
}
async function addImageFromUrl() {
const urlInput = document.getElementById('urlInput');
const url = urlInput.value.trim();
if (!url) {
showToast('Please enter an image URL');
return;
}
try {
// Validate the URL format
new URL(url);
const addButton = document.querySelector('.url-button');
addButton.disabled = true;
addButton.textContent = 'Loading...';
// Validate the image URL by loading it into an <img> element
await validateImageUrl(url);
// Add the image to the gallery
addImage({
id: Date.now(),
url: url
});
urlInput.value = '';
showToast('Image added successfully');
} catch (error) {
showToast(error.message || 'Failed to load image. Please check the URL.');
} finally {
const addButton = document.querySelector('.url-button');
addButton.disabled = false;
addButton.textContent = 'Add URL';
}
}
function validateImageUrl(url) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(true);
img.onerror = () => reject(new Error('Failed to load image. Please check the URL.'));
img.src = url;
});
}
function addImage(image) {
images.unshift(image);
renderGallery();
}
function deleteImage(id) {
images = images.filter(img => img.id !== id);
renderGallery();
showToast('Image deleted successfully');
}
function showModal(url) {
modalImage.src = url;
modal.classList.add('active');
}
modal.addEventListener('click', () => {
modal.classList.remove('active');
});
function renderGallery() {
gallery.innerHTML = images.map(img => `
<div class="image-card">
<img src="${img.url}" alt="Gallery image" onclick="showModal('${img.url}')">
<div class="image-overlay">
<button class="delete-button" onclick="deleteImage(${img.id})">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6L6 18M6 6l12 12"/>
</svg>
</button>
</div>
</div>
`).join('');
}
</script>
</body>
</html>