BruceSquared1's picture
Audience Awareness weekly
f6a23d2 verified
Raw History Blame Contribute Delete
5.12 kB
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');
});
}
});