Spaces:
Running
Running
Download script.js from Nico85525/cosmic-explorer: direct link, hf CLI and curl.
- Browser
- Download file 3.2 kB
-
https://huggingface.co/spaces/Nico85525/cosmic-explorer/resolve/main/script.js
- Command line
-
hf download hf://spaces/Nico85525/cosmic-explorer/script.js
-
curl -L -o script.js https://huggingface.co/spaces/Nico85525/cosmic-explorer/resolve/main/script.js
3.2 kB
| 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); | |
| }); | |
| }); |