File size: 2,570 Bytes
2352422 | 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 | document.addEventListener('DOMContentLoaded', async function() {
try {
// Fetch NASA APOD data
const response = await fetch('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&count=6');
const data = await response.json();
const galleryContainer = document.querySelector('#gallery .grid');
galleryContainer.innerHTML = ''; // Clear loading skeletons
data.forEach(item => {
const galleryItem = document.createElement('div');
galleryItem.className = 'gallery-item overflow-hidden rounded-xl relative group';
galleryItem.innerHTML = `
<div class="h-64 w-full relative">
<img src="${item.media_type === 'video' ? 'https://img.youtube.com/vi/' + item.url.split('=')[1] + '/maxresdefault.jpg' : item.hdurl || item.url}"
alt="${item.title}"
class="w-full h-full object-cover transition-transform duration-300 group-hover:scale-105">
<div class="absolute inset-0 bg-gradient-to-t from-black/70 via-transparent to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-end p-4">
<div>
<h3 class="text-lg font-semibold text-white">${item.title}</h3>
<p class="text-sm text-cosmic-200 line-clamp-2">${item.explanation}</p>
</div>
</div>
</div>
`;
galleryContainer.appendChild(galleryItem);
});
// Replace feather icons after content is loaded
feather.replace();
} catch (error) {
console.error('Error fetching NASA data:', error);
const galleryContainer = document.querySelector('#gallery .grid');
galleryContainer.innerHTML = `
<div class="col-span-full text-center py-12">
<i data-feather="alert-circle" class="w-12 h-12 mx-auto text-nebula-500 mb-4"></i>
<h3 class="text-xl font-medium text-cosmic-300">Failed to load cosmic images</h3>
<p class="text-nebula-300 mt-2">We're having trouble connecting to the cosmic database. Please try again later.</p>
<button onclick="window.location.reload()" class="mt-4 px-4 py-2 bg-cosmic-600 hover:bg-cosmic-500 rounded-lg transition-colors">
Retry
</button>
</div>
`;
feather.replace();
}
}); |