LuminaWebsite/assets/js/scroll-progress.js
Maaxxs 8d7b729744 Custom scroll bar: gold progress rail on the right instead of the browser scrollbar
- Thin rail that fills with gold while scrolling, click jumps to that position, drag scrolls along
- Hidden when the page fits on the screen, browser scrollbar hidden (stays without JavaScript)

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 02:26:59 +02:00

72 lines
2.7 KiB
JavaScript

/**
* Eigene Scroll-Leiste statt des Browser-Scrollbalkens: eine schmale Schiene am rechten Rand,
* die sich beim Runterscrollen golden fuellt. Klicken springt an die Stelle, Ziehen scrollt mit.
* Der Browser-Balken ist per CSS ausgeblendet (html.has-scroll-progress) - erst wenn dieses Skript
* laeuft, sonst bliebe ohne JavaScript gar keine Leiste uebrig.
*/
(function () {
'use strict';
var root = document.documentElement;
var rail = document.createElement('div');
rail.className = 'scroll-progress';
rail.setAttribute('aria-hidden', 'true');
rail.innerHTML = '<div class="scroll-progress-fill"></div>';
document.body.appendChild(rail);
root.classList.add('has-scroll-progress');
var fill = rail.firstChild;
var ticking = false;
function maxScroll() {
return Math.max(0, root.scrollHeight - window.innerHeight);
}
function update() {
ticking = false;
var max = maxScroll();
// Passt die Seite ganz auf den Bildschirm, braucht es keine Leiste
rail.classList.toggle('is-hidden', max < 4);
var progress = max > 0 ? Math.min(1, Math.max(0, window.scrollY / max)) : 0;
fill.style.transform = 'scaleY(' + progress + ')';
rail.classList.toggle('is-end', progress > 0.995);
}
function schedule() {
if (!ticking) {
ticking = true;
window.requestAnimationFrame(update);
}
}
window.addEventListener('scroll', schedule, { passive: true });
window.addEventListener('resize', schedule);
// Nachladende Bilder/Inhalte aendern die Seitenhoehe
if (window.ResizeObserver) {
new ResizeObserver(schedule).observe(document.body);
}
update();
// Klicken und Ziehen: Position auf der Schiene -> Stelle auf der Seite
function scrollToPointer(clientY, smooth) {
var rect = rail.getBoundingClientRect();
var ratio = Math.min(1, Math.max(0, (clientY - rect.top) / rect.height));
window.scrollTo({ top: ratio * maxScroll(), behavior: smooth ? 'smooth' : 'auto' });
}
rail.addEventListener('pointerdown', function (event) {
event.preventDefault();
rail.setPointerCapture(event.pointerId);
rail.classList.add('is-dragging');
scrollToPointer(event.clientY, true);
});
rail.addEventListener('pointermove', function (event) {
if (rail.classList.contains('is-dragging')) scrollToPointer(event.clientY, false);
});
function stop(event) {
rail.classList.remove('is-dragging');
if (rail.hasPointerCapture && rail.hasPointerCapture(event.pointerId)) rail.releasePointerCapture(event.pointerId);
}
rail.addEventListener('pointerup', stop);
rail.addEventListener('pointercancel', stop);
})();