File size: 5,123 Bytes
f6a23d2 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 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 = `
<div class="h-48 w-full overflow-hidden">
<img src="https://static.photos/technology/640x360/${imageSeed}" alt="Issue Image" class="w-full h-full object-cover transition-transform duration-500 hover:scale-110">
</div>
<div class="p-6 flex flex-col flex-1">
<div class="flex items-center justify-between mb-4">
<span class="text-xs font-bold text-indigo-600 uppercase tracking-wider">${category}</span>
<span class="text-xs text-slate-400">Issue #${100 + post.id}</span>
</div>
<h3 class="text-xl font-bold text-slate-900 mb-3 line-clamp-2 leading-snug hover:text-indigo-600 cursor-pointer transition-colors">
${post.title}
</h3>
<p class="text-slate-600 text-sm mb-6 line-clamp-3 flex-1">
${post.body}
</p>
<a href="#" class="inline-flex items-center text-sm font-bold text-slate-900 hover:text-indigo-600 mt-auto transition-colors">
Read Issue <i data-feather="arrow-right" class="w-4 h-4 ml-2"></i>
</a>
</div>
`;
grid.appendChild(card);
});
// Re-initialize feather icons for new content
feather.replace();
} catch (error) {
console.error("Error fetching issues:", error);
loading.innerHTML = '<p class="text-red-500">Failed to load issues. Please try again later.</p>';
}
};
// 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');
});
}
}); |