129 lines
4.6 KiB
JavaScript
129 lines
4.6 KiB
JavaScript
/**
|
|
* error-page.js — Thème + curseur personnalisé pour les pages d'erreur
|
|
* Autonome : ne dépend pas de main.js
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var html = document.documentElement;
|
|
|
|
/* ============================================
|
|
THEME SWITCH
|
|
============================================ */
|
|
|
|
function getStoredTheme() {
|
|
return localStorage.getItem('theme') || 'light';
|
|
}
|
|
|
|
function setTheme(theme) {
|
|
html.setAttribute('data-theme', theme);
|
|
localStorage.setItem('theme', theme);
|
|
}
|
|
|
|
var themeBtn = document.getElementById('themeSwitch');
|
|
|
|
if (themeBtn) {
|
|
themeBtn.addEventListener('click', function () {
|
|
var current = html.getAttribute('data-theme');
|
|
setTheme(current === 'dark' ? 'light' : 'dark');
|
|
});
|
|
}
|
|
|
|
/* Suppression de la classe no-transition après le premier rendu
|
|
(déjà ajoutée dans le <head> inline) */
|
|
requestAnimationFrame(function () {
|
|
requestAnimationFrame(function () {
|
|
html.classList.remove('no-transition');
|
|
});
|
|
});
|
|
|
|
/* ============================================
|
|
CUSTOM CURSOR — Dot + Ring
|
|
Actif uniquement sur devices à pointeur précis
|
|
============================================ */
|
|
|
|
var cursorDot = document.getElementById('cursorDot');
|
|
var cursorRing = document.getElementById('cursorRing');
|
|
var isPointer = window.matchMedia('(hover: hover) and (pointer: fine)').matches;
|
|
|
|
if (cursorDot && cursorRing && isPointer) {
|
|
|
|
var mouseX = 0, mouseY = 0;
|
|
var ringX = 0, ringY = 0;
|
|
var cursorVisible = false;
|
|
|
|
/* Suivi instantané du point */
|
|
document.addEventListener('mousemove', function (e) {
|
|
mouseX = e.clientX;
|
|
mouseY = e.clientY;
|
|
cursorDot.style.left = mouseX + 'px';
|
|
cursorDot.style.top = mouseY + 'px';
|
|
if (!cursorVisible) {
|
|
cursorVisible = true;
|
|
ringX = mouseX;
|
|
ringY = mouseY;
|
|
cursorDot.classList.add('is-visible');
|
|
cursorRing.classList.add('is-visible');
|
|
}
|
|
}, { passive: true });
|
|
|
|
/* Détection position initiale après Ctrl+R sans mouvement */
|
|
document.addEventListener('mouseover', function onFirstOver(e) {
|
|
if (!cursorVisible) {
|
|
mouseX = e.clientX;
|
|
mouseY = e.clientY;
|
|
cursorDot.style.left = mouseX + 'px';
|
|
cursorDot.style.top = mouseY + 'px';
|
|
ringX = mouseX;
|
|
ringY = mouseY;
|
|
cursorVisible = true;
|
|
cursorDot.classList.add('is-visible');
|
|
cursorRing.classList.add('is-visible');
|
|
}
|
|
document.removeEventListener('mouseover', onFirstOver);
|
|
}, { passive: true });
|
|
|
|
document.addEventListener('mouseleave', function () {
|
|
cursorDot.classList.remove('is-visible');
|
|
cursorRing.classList.remove('is-visible');
|
|
});
|
|
document.addEventListener('mouseenter', function () {
|
|
cursorDot.classList.add('is-visible');
|
|
cursorRing.classList.add('is-visible');
|
|
});
|
|
|
|
/* RAF : anneau avec lerp */
|
|
function animateCursor() {
|
|
requestAnimationFrame(animateCursor);
|
|
ringX += (mouseX - ringX) * 0.11;
|
|
ringY += (mouseY - ringY) * 0.11;
|
|
cursorRing.style.left = ringX + 'px';
|
|
cursorRing.style.top = ringY + 'px';
|
|
}
|
|
requestAnimationFrame(animateCursor);
|
|
|
|
/* Hover sur éléments interactifs */
|
|
var INTERACTIVE = 'a, button, input, textarea, select, label, [role="button"]';
|
|
document.querySelectorAll(INTERACTIVE).forEach(function (el) {
|
|
el.addEventListener('mouseenter', function () {
|
|
cursorDot.classList.add('is-hovering');
|
|
cursorRing.classList.add('is-hovering');
|
|
}, { passive: true });
|
|
el.addEventListener('mouseleave', function () {
|
|
cursorDot.classList.remove('is-hovering');
|
|
cursorRing.classList.remove('is-hovering');
|
|
}, { passive: true });
|
|
});
|
|
|
|
/* Clic */
|
|
document.addEventListener('mousedown', function () {
|
|
cursorDot.classList.add('is-clicking');
|
|
cursorRing.classList.add('is-clicking');
|
|
});
|
|
document.addEventListener('mouseup', function () {
|
|
cursorDot.classList.remove('is-clicking');
|
|
cursorRing.classList.remove('is-clicking');
|
|
});
|
|
}
|
|
|
|
})();
|