fix(mobile): enforce single-station snap per swipe and prevent runaway touch inertia

This commit is contained in:
2026-11-07 13:17:40 +05:30
parent 777863a635
commit f13b9bfb7c
+85 -15
View File
@@ -32,10 +32,13 @@
html{ html{
background:var(--bg);color:var(--ink); background:var(--bg);color:var(--ink);
overflow-x:hidden; overflow-x:hidden;
overscroll-behavior:none;
} }
body{ body{
font-family:var(--sans);background:var(--bg); font-family:var(--sans);background:var(--bg);
overflow-x:hidden;-webkit-font-smoothing:antialiased; overflow-x:hidden;-webkit-font-smoothing:antialiased;
overscroll-behavior:none;
touch-action:pan-y;
} }
a{color:inherit;text-decoration:none} a{color:inherit;text-decoration:none}
button{font-family:inherit;cursor:pointer;border:none;background:none} button{font-family:inherit;cursor:pointer;border:none;background:none}
@@ -841,6 +844,8 @@ var stationEls = STATIONS.map(function(s){ return document.getElementById(s.id);
Each scroll action smoothly travels to the next/prev Each scroll action smoothly travels to the next/prev
station, coming to a complete, stable pause at each one. station, coming to a complete, stable pause at each one.
═══════════════════════════════════════════════════ */ ═══════════════════════════════════════════════════ */
var isProgrammaticScroll = false;
function goToStation(idx){ function goToStation(idx){
idx = CL(Math.round(idx), 0, STATIONS.length - 1); idx = CL(Math.round(idx), 0, STATIONS.length - 1);
targetStationIdx = idx; targetStationIdx = idx;
@@ -850,8 +855,10 @@ function goToStation(idx){
var maxScroll = Math.max(1, SCROLL_HEIGHT - window.innerHeight); var maxScroll = Math.max(1, SCROLL_HEIGHT - window.innerHeight);
var targetY = (idx / (STATIONS.length - 1)) * maxScroll; var targetY = (idx / (STATIONS.length - 1)) * maxScroll;
isProgrammaticScroll = true;
window.scrollTo(0, targetY); window.scrollTo(0, targetY);
scrollY = targetY; scrollY = targetY;
setTimeout(function(){ isProgrammaticScroll = false; }, 120);
} }
function nextStation(){ function nextStation(){
@@ -905,28 +912,87 @@ window.addEventListener('wheel', function(e){
wheelDecay = setTimeout(function(){ wheelAccum = 0; }, 90); wheelDecay = setTimeout(function(){ wheelAccum = 0; }, 90);
}, {passive: false}); }, {passive: false});
/* Touch gestures: swipe up/down */ /* Touch gestures: swipe up/down with precise single-station snapping */
var touchStartX = 0;
var touchStartY = 0; var touchStartY = 0;
var touchStartTime = 0;
var touchIsActive = false;
var touchTriggered = false;
window.addEventListener('touchstart', function(e){ window.addEventListener('touchstart', function(e){
if(e.touches.length !== 1) return;
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY; touchStartY = e.touches[0].clientY;
touchStartTime = Date.now();
touchIsActive = true;
touchTriggered = false;
}, {passive: true}); }, {passive: true});
window.addEventListener('touchend', function(e){ window.addEventListener('touchmove', function(e){
var dy = touchStartY - e.changedTouches[0].clientY; if(e.touches.length !== 1) return;
var now = Date.now(); var dx = e.touches[0].clientX - touchStartX;
var dir = dy > 0 ? 1 : -1; var dy = e.touches[0].clientY - touchStartY;
var isDirChange = (dir !== lastNavDir);
var activeCooldown = isDirChange ? 180 : 380;
if(Math.abs(dy) > 28 && (now - lastNavTime) > activeCooldown){ // Intercept vertical swipes immediately so browser never starts kinetic momentum scroll
lastNavDir = dir; if(Math.abs(dy) > 7 && Math.abs(dy) > Math.abs(dx)){
if(dy > 0){ if(e.cancelable){
nextStation(); e.preventDefault();
} else { }
prevStation();
// Trigger station switch as soon as user has swiped past threshold (36px)
if(!touchTriggered && Math.abs(dy) > 36){
var now = Date.now();
var dir = dy < 0 ? 1 : -1;
var isDirChange = (dir !== lastNavDir);
var activeCooldown = isDirChange ? 150 : 320;
if((now - lastNavTime) > activeCooldown){
touchTriggered = true;
lastNavDir = dir;
if(dir > 0){
nextStation();
} else {
prevStation();
}
}
} }
lastNavTime = now;
} }
}, {passive: false});
window.addEventListener('touchend', function(e){
touchIsActive = false;
if(touchTriggered) {
touchTriggered = false;
return;
}
if(!e.changedTouches || e.changedTouches.length === 0) return;
var dy = touchStartY - e.changedTouches[0].clientY;
var dx = touchStartX - e.changedTouches[0].clientX;
var dt = Date.now() - touchStartTime;
// Fast flick gesture support (short distance but quick flick)
if(Math.abs(dy) > 18 && Math.abs(dy) > Math.abs(dx) && dt < 280){
var now = Date.now();
var dir = dy > 0 ? 1 : -1;
var isDirChange = (dir !== lastNavDir);
var activeCooldown = isDirChange ? 150 : 300;
if((now - lastNavTime) > activeCooldown){
lastNavDir = dir;
if(dir > 0){
nextStation();
} else {
prevStation();
}
}
}
touchTriggered = false;
}, {passive: true});
window.addEventListener('touchcancel', function(){
touchIsActive = false;
touchTriggered = false;
}, {passive: true}); }, {passive: true});
/* Keyboard: arrows, space, page down/up */ /* Keyboard: arrows, space, page down/up */
@@ -1138,10 +1204,14 @@ function loop(){
} }
window.addEventListener('scroll', function(){ window.addEventListener('scroll', function(){
if(isProgrammaticScroll) return;
// Ignore native scroll events during touch gestures or on mobile screens to prevent kinetic scroll runaways
if(touchIsActive || window.innerWidth <= 900) return;
var maxScroll = Math.max(1, SCROLL_HEIGHT - window.innerHeight); var maxScroll = Math.max(1, SCROLL_HEIGHT - window.innerHeight);
var scrollFrac = window.scrollY / maxScroll; var scrollFrac = window.scrollY / maxScroll;
var approxIdx = Math.round(scrollFrac * (STATIONS.length - 1)); var approxIdx = Math.round(scrollFrac * (STATIONS.length - 1));
if(approxIdx !== targetStationIdx && (Date.now() - lastNavTime) > 300){ if(approxIdx !== targetStationIdx && (Date.now() - lastNavTime) > 350){
goToStation(approxIdx); goToStation(approxIdx);
} }
}, {passive:true}); }, {passive:true});