Add scroll slider with section dots

This commit is contained in:
BlackAngelTV committed 2026-05-11 13:39:39 +02:00
1 parent 0fffed7837
commit 30f447d932
3 files changed
+410 -36

No files matched your search

+180 -1
View File
@@ -6,4 +6,183 @@ MENUBUTTON.addEventListener("click", toggleNav)
function toggleNav(){
MENUBUTTON.classList.toggle("active")
NAVIGATION.classList.toggle("active")
}
}
// Scroll Slider (3 sections)
document.addEventListener('DOMContentLoaded', () => {
const sections = [
document.getElementById('hero-section'),
document.getElementById('pricing'),
document.getElementById('footer-section')
];
const dots = [
document.getElementById('dot-top'),
document.getElementById('dot-mid'),
document.getElementById('dot-bottom')
];
if (!sections[0] || !sections[1] || !sections[2]) {
return;
}
const hashMap = ['', '#pricing', '#footer'];
let current = 0;
let isAnimating = false;
function clean(section) {
section.classList.remove('slide-in-up', 'slide-out-up', 'slide-out-down');
}
function applyStateInstant(index) {
sections.forEach((section, i) => {
section.style.transition = 'none';
clean(section);
if (i < index) {
section.classList.add('slide-out-up');
} else if (i === index) {
section.classList.add('slide-in-up');
}
});
requestAnimationFrame(() => requestAnimationFrame(() => {
sections.forEach(section => { section.style.transition = ''; });
}));
}
function updateDots() {
dots.forEach((dot, i) => {
if (!dot) {
return;
}
dot.classList.toggle('is-filled', i === current);
dot.classList.toggle('is-hollow', i !== current);
});
}
function updateHash() {
const hash = hashMap[current];
history.replaceState(null, '', hash || window.location.pathname);
}
function goTo(target) {
if (isAnimating || target === current) {
return;
}
if (target < 0 || target > sections.length - 1) {
return;
}
isAnimating = true;
const prev = current;
current = target;
if (target > prev) {
clean(sections[prev]);
sections[prev].classList.add('slide-out-up');
setTimeout(() => {
clean(sections[target]);
sections[target].classList.add('slide-in-up');
}, 80);
} else {
clean(sections[prev]);
sections[prev].classList.add('slide-out-down');
setTimeout(() => {
clean(sections[target]);
sections[target].classList.add('slide-in-up');
}, 80);
}
updateDots();
updateHash();
setTimeout(() => { isAnimating = false; }, 750);
}
function init() {
const hash = window.location.hash;
if (hash === '#pricing') {
current = 1;
} else if (hash === '#footer') {
current = 2;
} else {
current = 0;
}
applyStateInstant(current);
updateDots();
}
const btnToPricing = document.getElementById('btn-to-pricing');
const btnToHero = document.getElementById('btn-to-hero');
const btnToFooter = document.getElementById('btn-to-footer');
const btnFooterToPricing = document.getElementById('btn-footer-to-pricing');
if (btnToPricing) btnToPricing.addEventListener('click', () => goTo(1));
if (btnToHero) btnToHero.addEventListener('click', () => goTo(0));
if (btnToFooter) btnToFooter.addEventListener('click', () => goTo(2));
if (btnFooterToPricing) btnFooterToPricing.addEventListener('click', () => goTo(1));
dots.forEach((dot, i) => {
if (!dot) {
return;
}
dot.addEventListener('click', () => goTo(i));
});
window.addEventListener('wheel', (event) => {
if (isAnimating) {
return;
}
if (event.deltaY > 30) {
goTo(current + 1);
} else if (event.deltaY < -30) {
goTo(current - 1);
}
}, { passive: true });
let touchStartY = 0;
window.addEventListener('touchstart', (event) => {
touchStartY = event.touches[0].clientY;
}, { passive: true });
window.addEventListener('touchend', (event) => {
if (isAnimating) {
return;
}
const delta = touchStartY - event.changedTouches[0].clientY;
if (delta > 50) {
goTo(current + 1);
} else if (delta < -50) {
goTo(current - 1);
}
}, { passive: true });
window.addEventListener('keydown', (event) => {
if (isAnimating) {
return;
}
if (event.key === 'ArrowDown' || event.key === 'PageDown') {
goTo(current + 1);
}
if (event.key === 'ArrowUp' || event.key === 'PageUp') {
goTo(current - 1);
}
});
window.addEventListener('hashchange', () => {
const hash = window.location.hash;
const target = hash === '#pricing' ? 1 : hash === '#footer' ? 2 : 0;
goTo(target);
});
init();
});