document.addEventListener('DOMContentLoaded', () => { // Simulate Number Counter Animation for Stats const animateValue = (id, start, end, duration, suffix = "") => { const obj = document.getElementById(id); if (!obj) return; let startTimestamp = null; const step = (timestamp) => { if (!startTimestamp) startTimestamp = timestamp; const progress = Math.min((timestamp - startTimestamp) / duration, 1); const value = Math.floor(progress * (end - start) + start); obj.innerHTML = value.toLocaleString() + suffix; if (progress < 1) { window.requestAnimationFrame(step); } }; window.requestAnimationFrame(step); }; // Observer for Stats Animation const statsSection = document.querySelector('.bg-white.border-y'); // Target stats section let statsAnimated = false; const statsObserver = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting && !statsAnimated) { animateValue("stat-subscribers", 0, 15420, 2000); animateValue("stat-openrate", 0, 68, 2500, "%"); animateValue("stat-issues", 0, 124, 1500); statsAnimated = true; } }); }, { threshold: 0.5 }); if(statsSection) { statsObserver.observe(statsSection); } // Fetch Data for Weekly Issues (Using JSONPlaceholder) const loadIssues = async () => { const grid = document.getElementById('issues-grid'); const loading = document.getElementById('loading'); try { // Fetch 6 posts to simulate newsletter issues const response = await fetch('https://jsonplaceholder.typicode.com/posts?_limit=6'); const data = await response.json(); loading.classList.add('hidden'); grid.classList.remove('hidden'); data.forEach((post, index) => { // Randomly assign a category for visual variety const categories = ['Strategy', 'Data', 'Psychology', 'Case Study']; const category = categories[index % categories.length]; const imageSeed = 50 + index; // Distinct images const card = document.createElement('article'); card.className = 'bg-white rounded-2xl overflow-hidden shadow-sm border border-slate-100 card-hover flex flex-col h-full opacity-0'; card.style.animation = `fadeInUp 0.5s ease-out forwards ${index * 0.1}s`; card.innerHTML = `
Issue Image
${category} Issue #${100 + post.id}

${post.title}

${post.body}

Read Issue
`; grid.appendChild(card); }); // Re-initialize feather icons for new content feather.replace(); } catch (error) { console.error("Error fetching issues:", error); loading.innerHTML = '

Failed to load issues. Please try again later.

'; } }; // Trigger Load const archiveSection = document.getElementById('archive'); const archiveObserver = new IntersectionObserver((entries) => { if (entries[0].isIntersecting) { loadIssues(); archiveObserver.disconnect(); } }, { rootMargin: '200px' }); if(archiveSection) { archiveObserver.observe(archiveSection); } // Load More Button (Simulation) const loadMoreBtn = document.getElementById('load-more'); if(loadMoreBtn) { loadMoreBtn.addEventListener('click', () => { loadMoreBtn.textContent = "You reached the end!"; loadMoreBtn.disabled = true; loadMoreBtn.classList.add('bg-slate-100'); }); } });