// Intersection Observer for scroll animations
document.addEventListener('DOMContentLoaded', () => {
const sections = document.querySelectorAll('section');
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('in-view');
observer.unobserve(entry.target);
}
});
}, observerOptions);
sections.forEach(section => {
observer.observe(section);
});
// Mobile menu toggle
const mobileMenuButton = document.getElementById('mobile-menu-button');
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener('click', () => {
mobileMenu.classList.toggle('hidden');
const isExpanded = mobileMenuButton.getAttribute('aria-expanded') === 'true';
mobileMenuButton.setAttribute('aria-expanded', !isExpanded);
});
}
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetElement = document.querySelector(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
// Update URL without jumping
history.pushState(null, null, targetId);
}
});
});
});
// Audio player functionality
class AudioPlayer {
constructor(containerId) {
this.container = document.getElementById(containerId);
this.audio = this.container.querySelector('audio');
this.playButton = this.container.querySelector('.play-button');
this.progress = this.container.querySelector('.progress-bar');
this.currentTimeDisplay = this.container.querySelector('.current-time');
this.durationDisplay = this.container.querySelector('.duration');
if (this.audio && this.playButton) {
this.init();
}
}
init() {
this.playButton.addEventListener('click', () => this.togglePlay());
this.audio.addEventListener('timeupdate', () => this.updateProgress());
this.audio.addEventListener('loadedmetadata', () => this.setDuration());
this.audio.addEventListener('ended', () => this.resetPlayer());
// Progress bar click to seek
this.progress.addEventListener('click', (e) => {
const rect = this.progress.getBoundingClientRect();
const pos = (e.clientX - rect.left) / rect.width;
this.audio.currentTime = pos * this.audio.duration;
});
}
togglePlay() {
if (this.audio.paused) {
this.audio.play();
this.playButton.innerHTML = '';
feather.replace();
} else {
this.audio.pause();
this.playButton.innerHTML = '';
feather.replace();
}
}
updateProgress() {
const percent = (this.audio.currentTime / this.audio.duration) * 100;
this.progress.style.width = `${percent}%`;
this.currentTimeDisplay.textContent = this.formatTime(this.audio.currentTime);
}
setDuration() {
this.durationDisplay.textContent = this.formatTime(this.audio.duration);
}
resetPlayer() {
this.progress.style.width = '0%';
this.playButton.innerHTML = '';
feather.replace();
this.currentTimeDisplay.textContent = this.formatTime(0);
}
formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
}
}
// Initialize audio players on the page
document.addEventListener('DOMContentLoaded', () => {
const audioPlayers = document.querySelectorAll('.audio-player');
audioPlayers.forEach((player, index) => {
new AudioPlayer(player.id || `audio-player-${index}`);
});
});