naughty-nook-nexus / script.js
thetwistedpixie's picture
Make it more cheekier and naughty
859918b verified
// Global JavaScript functionality
document.addEventListener('DOMContentLoaded', function() {
// Add smooth scrolling to all links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// Add intersection observer for fade-in animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in');
}
});
}, observerOptions);
// Observe all sections for animation
document.querySelectorAll('section').forEach(section => {
observer.observe(section);
});
// Handle mobile menu toggle
window.toggleMobileMenu = function() {
const mobileMenu = document.getElementById('mobileMenu');
if (mobileMenu) {
mobileMenu.classList.toggle('hidden');
mobileMenu.classList.toggle('flex');
}
};
// Cookie consent handling
window.handleCookieConsent = function(action) {
const consentBanner = document.getElementById('cookieConsent');
if (consentBanner) {
consentBanner.classList.add('hidden');
}
if (action === 'accept') {
localStorage.setItem('cookieConsent', 'accepted');
} else if (action === 'reject') {
localStorage.setItem('cookieConsent', 'rejected');
}
};
// Check for existing cookie consent
if (localStorage.getItem('cookieConsent')) {
const consentBanner = document.getElementById('cookieConsent');
if (consentBanner) {
consentBanner.classList.add('hidden');
}
}
});
// Utility function for API calls
async function apiCall(endpoint, options = {}) {
try {
const response = await fetch(endpoint, {
headers: {
'Content-Type': 'application/json',
...options.headers
},
...options
});
if (!response.ok) {
throw new Error(`API call failed: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error('API call error:', error);
return null;
}
}