First Version
This commit is contained in:
230
js/main.js
Normal file
230
js/main.js
Normal file
@@ -0,0 +1,230 @@
|
||||
/**
|
||||
* ArcaneNeko Website - Main Initialization
|
||||
*
|
||||
* Handles: mobile menu, scroll reveal, stat counters,
|
||||
* back-to-top, scroll progress, smooth scroll
|
||||
*/
|
||||
|
||||
// ----------------------
|
||||
// Mobile Hamburger Menu
|
||||
// ----------------------
|
||||
function initMobileMenu() {
|
||||
const hamburger = document.getElementById('hamburger');
|
||||
const mobileMenu = document.getElementById('mobileMenu');
|
||||
if (!hamburger || !mobileMenu) return;
|
||||
|
||||
hamburger.addEventListener('click', () => {
|
||||
const open = hamburger.classList.toggle('open');
|
||||
mobileMenu.classList.toggle('open', open);
|
||||
hamburger.setAttribute('aria-expanded', String(open));
|
||||
});
|
||||
|
||||
mobileMenu.querySelectorAll('a').forEach(link => {
|
||||
link.addEventListener('click', () => {
|
||||
hamburger.classList.remove('open');
|
||||
mobileMenu.classList.remove('open');
|
||||
hamburger.setAttribute('aria-expanded', 'false');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Scroll Reveal (cards)
|
||||
// ----------------------
|
||||
function initScrollReveal() {
|
||||
const observer = new IntersectionObserver(entries => {
|
||||
entries.forEach(entry => {
|
||||
if (entry.isIntersecting) {
|
||||
entry.target.classList.add('visible');
|
||||
observer.unobserve(entry.target);
|
||||
}
|
||||
});
|
||||
}, { threshold: 0.1 });
|
||||
|
||||
document.querySelectorAll('.reveal').forEach(el => observer.observe(el));
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Stat Counter Animation
|
||||
// ----------------------
|
||||
function animateCount(el, target, suffix, duration) {
|
||||
const start = performance.now();
|
||||
const from = 0;
|
||||
|
||||
function step(now) {
|
||||
const elapsed = now - start;
|
||||
const progress = Math.min(elapsed / duration, 1);
|
||||
const eased = 1 - Math.pow(1 - progress, 3);
|
||||
const current = Math.round(from + (target - from) * eased);
|
||||
el.textContent = current + suffix;
|
||||
if (progress < 1) requestAnimationFrame(step);
|
||||
}
|
||||
requestAnimationFrame(step);
|
||||
}
|
||||
|
||||
function initStatCounters() {
|
||||
const statsBar = document.querySelector('.stats-bar');
|
||||
if (!statsBar) return;
|
||||
|
||||
const observer = new IntersectionObserver(entries => {
|
||||
if (!entries[0].isIntersecting) return;
|
||||
observer.disconnect();
|
||||
|
||||
document.querySelectorAll('.stat').forEach(el => el.classList.add('animated'));
|
||||
|
||||
document.querySelectorAll('.stat-value[data-count]').forEach(el => {
|
||||
const target = parseFloat(el.dataset.count);
|
||||
const suffix = el.dataset.suffix || '';
|
||||
const duration = 1400;
|
||||
animateCount(el, target, suffix, duration);
|
||||
});
|
||||
|
||||
}, { threshold: 0.5 });
|
||||
|
||||
observer.observe(statsBar);
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Back to Top Button
|
||||
// ----------------------
|
||||
function initBackToTop() {
|
||||
const btn = document.getElementById('backToTop');
|
||||
if (!btn) return;
|
||||
|
||||
window.addEventListener('scroll', () => {
|
||||
btn.classList.toggle('visible', window.scrollY > 450);
|
||||
}, { passive: true });
|
||||
|
||||
btn.addEventListener('click', () => {
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
});
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Scroll Progress Bar (legal pages)
|
||||
// ----------------------
|
||||
function initScrollProgress() {
|
||||
const bar = document.getElementById('scrollProgress');
|
||||
if (!bar) return;
|
||||
|
||||
const updateProgress = () => {
|
||||
const total = document.documentElement.scrollHeight - window.innerHeight;
|
||||
const progress = total > 0 ? (window.scrollY / total) * 100 : 0;
|
||||
const rounded = Math.round(progress);
|
||||
bar.style.width = progress + '%';
|
||||
bar.setAttribute('aria-valuenow', String(rounded));
|
||||
};
|
||||
|
||||
updateProgress();
|
||||
window.addEventListener('scroll', updateProgress, { passive: true });
|
||||
window.addEventListener('resize', updateProgress);
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Navbar scroll shadow
|
||||
// ----------------------
|
||||
function initNavShadow() {
|
||||
const navbar = document.querySelector('.navbar');
|
||||
if (!navbar) return;
|
||||
window.addEventListener('scroll', () => {
|
||||
navbar.style.boxShadow = window.scrollY > 10
|
||||
? '0 4px 24px rgba(0,0,0,0.3)'
|
||||
: 'none';
|
||||
}, { passive: true });
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Smooth scroll (hash links)
|
||||
// ----------------------
|
||||
function initSmoothScroll() {
|
||||
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
|
||||
anchor.addEventListener('click', e => {
|
||||
const href = anchor.getAttribute('href');
|
||||
if (href === '#') return;
|
||||
const target = document.querySelector(href);
|
||||
if (target) {
|
||||
e.preventDefault();
|
||||
const offset = 80;
|
||||
window.scrollTo({
|
||||
top: target.getBoundingClientRect().top + window.scrollY - offset,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Legal page TOC highlight
|
||||
// ----------------------
|
||||
function initTocHighlight() {
|
||||
const sections = document.querySelectorAll('.legal-section');
|
||||
const tocLinks = document.querySelectorAll('.legal-toc a');
|
||||
if (!sections.length || !tocLinks.length) return;
|
||||
|
||||
const observer = new IntersectionObserver(entries => {
|
||||
entries.forEach(entry => {
|
||||
if (entry.isIntersecting) {
|
||||
tocLinks.forEach(l => l.classList.remove('active'));
|
||||
const active = document.querySelector(`.legal-toc a[href="#${entry.target.id}"]`);
|
||||
if (active) active.classList.add('active');
|
||||
}
|
||||
});
|
||||
}, { rootMargin: '-20% 0px -70% 0px' });
|
||||
|
||||
sections.forEach(s => observer.observe(s));
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Legal page mobile TOC
|
||||
// ----------------------
|
||||
function initLegalTocToggle() {
|
||||
document.querySelectorAll('.legal-toc').forEach(toc => {
|
||||
const button = toc.querySelector('.legal-toc-toggle');
|
||||
const panel = toc.querySelector('.legal-toc-panel');
|
||||
if (!button || !panel) return;
|
||||
|
||||
button.addEventListener('click', () => {
|
||||
const open = toc.classList.toggle('open');
|
||||
button.setAttribute('aria-expanded', String(open));
|
||||
});
|
||||
|
||||
panel.querySelectorAll('a').forEach(link => {
|
||||
link.addEventListener('click', () => {
|
||||
if (window.innerWidth <= 768) {
|
||||
toc.classList.remove('open');
|
||||
button.setAttribute('aria-expanded', 'false');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
// Init on DOM ready
|
||||
// ----------------------
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// Apply saved theme before anything renders
|
||||
if (window.ThemeUtils) {
|
||||
window.ThemeUtils.applyTheme(window.ThemeUtils.getCurrentTheme());
|
||||
}
|
||||
|
||||
// Theme toggle
|
||||
document.querySelectorAll('.theme-toggle').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
if (window.ThemeUtils) {
|
||||
window.ThemeUtils.applyTheme(window.ThemeUtils.nextTheme());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
initMobileMenu();
|
||||
initScrollReveal();
|
||||
initStatCounters();
|
||||
initBackToTop();
|
||||
initScrollProgress();
|
||||
initNavShadow();
|
||||
initSmoothScroll();
|
||||
initTocHighlight();
|
||||
initLegalTocToggle();
|
||||
});
|
||||
44
js/theme.js
Normal file
44
js/theme.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Theme Switcher Utility
|
||||
*/
|
||||
|
||||
// Theme configuration
|
||||
const THEMES = ['crimson', 'dark', 'light'];
|
||||
|
||||
const THEME_ICONS = {
|
||||
crimson: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" width="16" height="16" aria-hidden="true"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>`,
|
||||
dark: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" width="16" height="16" aria-hidden="true"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg>`,
|
||||
light: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" width="16" height="16" aria-hidden="true"><path d="M12 2c0 0-4 4-4 8a4 4 0 0 0 8 0c0-4-4-8-4-8z"/></svg>`,
|
||||
};
|
||||
|
||||
const THEME_LABELS = {
|
||||
crimson: 'Switch to dark mode',
|
||||
dark: 'Switch to light mode',
|
||||
light: 'Switch to crimson theme',
|
||||
};
|
||||
|
||||
let currentTheme = localStorage.getItem('an-theme') || 'crimson';
|
||||
|
||||
function applyTheme(theme) {
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
document.querySelectorAll('.theme-toggle').forEach(btn => {
|
||||
btn.innerHTML = THEME_ICONS[theme];
|
||||
btn.setAttribute('title', THEME_LABELS[theme]);
|
||||
btn.setAttribute('aria-label', THEME_LABELS[theme]);
|
||||
});
|
||||
localStorage.setItem('an-theme', theme);
|
||||
currentTheme = theme;
|
||||
}
|
||||
|
||||
function nextTheme() {
|
||||
const idx = THEMES.indexOf(currentTheme);
|
||||
return THEMES[(idx + 1) % THEMES.length];
|
||||
}
|
||||
|
||||
// Export for use in other files
|
||||
window.ThemeUtils = {
|
||||
THEMES,
|
||||
applyTheme,
|
||||
nextTheme,
|
||||
getCurrentTheme: () => currentTheme,
|
||||
};
|
||||
Reference in New Issue
Block a user