Spaces:
Running
Running
File size: 3,199 Bytes
947dfa4 | 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 | document.addEventListener('DOMContentLoaded', function() {
const featuredImagesContainer = document.getElementById('featured-images');
const exploreBtn = document.getElementById('exploreBtn');
// Loading spinner
function showLoading() {
featuredImagesContainer.innerHTML = `
<div class="col-span-full flex justify-center my-16">
<div class="loading-spinner"></div>
</div>
`;
}
// Fetch NASA images
async function fetchNASAImages() {
showLoading();
try {
const response = await fetch('https://images-api.nasa.gov/search?q=galaxy&media_type=image');
const data = await response.json();
if (data.collection && data.collection.items) {
displayImages(data.collection.items.slice(0, 6));
} else {
featuredImagesContainer.innerHTML = '<p class="col-span-full text-center text-red-400">Failed to load images. Please try again later.</p>';
}
} catch (error) {
console.error('Error fetching NASA images:', error);
featuredImagesContainer.innerHTML = '<p class="col-span-full text-center text-red-400">Error loading images. Check your connection.</p>';
}
}
// Display images in grid
function displayImages(images) {
featuredImagesContainer.innerHTML = '';
images.forEach((item, index) => {
const imageData = item.data[0];
const imageUrl = item.links ? item.links[0].href : 'http://static.photos/space/640x360/' + (index + 1);
const card = document.createElement('div');
card.className = 'image-card overflow-hidden';
card.innerHTML = `
<div class="relative group">
<img src="${imageUrl}" alt="${imageData.title || 'Space Image'}" class="w-full h-64 object-cover">
<div class="absolute inset-0 bg-gradient-to-t from-black to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-end p-4">
<div>
<h3 class="font-bold text-lg">${imageData.title || 'Untitled'}</h3>
<p class="text-sm text-gray-300">${new Date(imageData.date_created).getFullYear() || 'Unknown date'}</p>
</div>
</div>
</div>
<div class="p-4">
<p class="text-gray-400 text-sm line-clamp-2">${imageData.description || 'No description available'}</p>
</div>
`;
featuredImagesContainer.appendChild(card);
});
}
// Initialize
fetchNASAImages();
// Button event
exploreBtn.addEventListener('click', function() {
this.innerHTML = 'Loading...';
setTimeout(() => {
this.innerHTML = 'Explore Gallery';
window.scrollTo({
top: document.querySelector('#featured-images').offsetTop - 100,
behavior: 'smooth'
});
}, 500);
});
}); |