Align the page to nearby carousels before wheel slide navigation

This commit is contained in:
2026-11-12 18:59:50 +03:00
parent ffe83740e1
commit 4c5095e62a
5 changed files with 102 additions and 14 deletions
+25 -7
View File
@@ -7,8 +7,9 @@ function fixture({width=390,size=330,length=5,platform='Win32',fine=true,reduced
const timers=new Map();let timer=0, resize;
const frames=new Map();let frameId=0;
let now=1000;
const mediaRect={top:100,bottom:900,height:800};
const target=()=>({listeners:{},dataset:{},classList:{toggle(){},add(){},remove(){}},addEventListener(n,f){(this.listeners[n]??=[]).push(f);},emit(n,e={}){for(const f of this.listeners[n]||[])f(e);}});
const mediaRect={top:116,bottom:916,height:800};
const target=()=>({listeners:{},dataset:{},classList:{toggle(){},add(){},remove(){}},addEventListener(n,f){(this.listeners[n]??=[]).push(f);},removeEventListener(n,f){this.listeners[n]=(this.listeners[n]||[]).filter(fn=>fn!==f);},emit(n,e={}){for(const f of this.listeners[n]||[])f(e);}});
const page={...target(),innerHeight:1000,scrollY:1000,scrollTo({top}){const delta=top-this.scrollY;this.scrollY=top;mediaRect.top-=delta;mediaRect.bottom-=delta;}};
const images=Array.from({length},()=>({...target(),tagName:'IMG'}));
const root={...target(),clientWidth:width};
const items=images.map((image,i)=>({...target(),get offsetWidth(){return size;},get offsetLeft(){return (root.clientWidth-size)/2+i*(size+24);},querySelector(){return {getBoundingClientRect:()=>mediaRect};},querySelectorAll(){return [];}}));
@@ -17,8 +18,8 @@ function fixture({width=390,size=330,length=5,platform='Win32',fine=true,reduced
const prev=target(),next=target(),count={},controls={};
root.querySelector=s=>({'[data-carousel-track]':track,'[data-carousel-prev]':prev,'[data-carousel-next]':next,'[data-carousel-count]':count,'[data-carousel-controls]':controls}[s]);
root.querySelectorAll=()=>[];
vm.runInNewContext(source,{requestAnimationFrame:f=>{frames.set(++frameId,f);return frameId;},cancelAnimationFrame:id=>frames.delete(id),navigator:{platform},performance:{now:()=>now},matchMedia:q=>({matches:q.includes('reduced-motion')?reduced:fine}),getComputedStyle:()=>({columnGap:'24'}),document:{readyState:'complete',querySelectorAll:()=>[root]},window:{...target(),innerHeight:1000},setTimeout:f=>{timers.set(++timer,f);return timer;},clearTimeout:id=>timers.delete(id),ResizeObserver:class{constructor(f){resize=f;}observe(){}},IntersectionObserver:class{observe(){}}});
return {root,track,images,prev,next,count,calls,mediaRect,frames,resize:()=>resize(),tick(ms){now+=ms;const pending=[...frames.values()];frames.clear();pending.forEach(f=>f(now));},advance(ms=300){now+=ms;},wheel(props={}){const e={deltaX:0,deltaY:100,deltaMode:0,cancelable:true,defaultPrevented:false,stopped:false,target:{closest(){return null;}},preventDefault(){this.defaultPrevented=true;},stopPropagation(){this.stopped=true;},...props};track.emit('wheel',e);return e;},settle(){const pending=[...timers.values()];timers.clear();pending.forEach(f=>f());},swipe(left){track.scrollLeft=left;track.emit('scroll');}};
vm.runInNewContext(source,{requestAnimationFrame:f=>{frames.set(++frameId,f);return frameId;},cancelAnimationFrame:id=>frames.delete(id),navigator:{platform},performance:{now:()=>now},matchMedia:q=>({matches:q.includes('reduced-motion')?reduced:fine}),getComputedStyle:()=>({columnGap:'24'}),document:{documentElement:{scrollHeight:6000},readyState:'complete',querySelectorAll:()=>[root]},window:page,setTimeout:f=>{timers.set(++timer,f);return timer;},clearTimeout:id=>timers.delete(id),ResizeObserver:class{constructor(f){resize=f;}observe(){}},IntersectionObserver:class{observe(){}}});
return {root,track,images,prev,next,count,calls,mediaRect,frames,page,resize:()=>resize(),tick(ms){now+=ms;const pending=[...frames.values()];frames.clear();pending.forEach(f=>f(now));},advance(ms=300){now+=ms;},wheel(props={}){const e={deltaX:0,deltaY:100,deltaMode:0,cancelable:true,defaultPrevented:false,stopped:false,target:{closest(){return null;}},preventDefault(){this.defaultPrevented=true;},stopPropagation(){this.stopped=true;},...props};track.emit('wheel',e);return e;},settle(){const pending=[...timers.values()];timers.clear();pending.forEach(f=>f());},swipe(left){track.scrollLeft=left;track.emit('scroll');}};
}
test('mouse wheel steps once per burst, reverses, and releases page at both ends',()=>{
@@ -44,13 +45,30 @@ test('pointer interaction cancels wheel motion and restores native snapping',()=
f.track.emit('pointerdown',{pointerType:'touch'});f.tick(500);
assert.equal(f.track.scrollLeft,left);assert.equal(f.frames.size,0);assert.equal(f.track.style.scrollSnapType,'');
});
test('partial galleries and uninterrupted page scrolling do not capture the wheel',()=>{
const f=fixture();f.mediaRect.top=350;f.mediaRect.bottom=1150;
test('distant galleries and uninterrupted page scrolling do not capture the wheel',()=>{
const f=fixture();f.mediaRect.top=500;f.mediaRect.bottom=1300;
assert.equal(f.wheel().defaultPrevented,false);assert.equal(f.count.textContent,'1 / 5');
f.mediaRect.top=100;f.mediaRect.bottom=900;f.advance(80);
f.mediaRect.top=116;f.mediaRect.bottom=916;f.advance(80);
assert.equal(f.wheel().defaultPrevented,false,'do not take over the ongoing page gesture');
f.advance(200);assert.equal(f.wheel().defaultPrevented,true);assert.equal(f.count.textContent,'2 / 5');
});
test('page magnet aligns media smoothly before allowing a fresh gesture to advance slides',()=>{
const f=fixture();f.mediaRect.top=250;f.mediaRect.bottom=1050;
assert.equal(f.wheel().defaultPrevented,true);assert.equal(f.count.textContent,'1 / 5');
f.tick(100);assert.ok(f.page.scrollY>1000&&f.page.scrollY<1134);
f.wheel();assert.equal(f.count.textContent,'1 / 5','arrival burst cannot advance slides');
f.tick(200);assert.equal(f.page.scrollY,1134);assert.equal(f.mediaRect.top,116);
f.wheel();assert.equal(f.count.textContent,'1 / 5','tail immediately after arrival is consumed');
f.advance(200);f.wheel();assert.equal(f.count.textContent,'2 / 5');
assert.equal(f.page.listeners.keydown.length,0,'temporary input hooks are cleaned up');
});
test('reverse or keyboard input cancels the page magnet; end slides never pull the page back',()=>{
const f=fixture();f.mediaRect.top=250;f.mediaRect.bottom=1050;f.wheel();f.tick(100);
const y=f.page.scrollY;assert.equal(f.wheel({deltaY:-100}).defaultPrevented,false);f.tick(500);assert.equal(f.page.scrollY,y);
const g=fixture();g.mediaRect.top=250;g.mediaRect.bottom=1050;g.wheel();g.tick(50);g.page.emit('keydown',{type:'keydown'});const stopped=g.page.scrollY;g.tick(400);assert.equal(g.page.scrollY,stopped);assert.equal(g.frames.size,0);
const end=fixture({length:2});end.next.emit('click');end.mediaRect.top=250;end.mediaRect.bottom=1050;
assert.equal(end.wheel().defaultPrevented,false);assert.equal(end.page.scrollY,1000);
});
test('partially exited galleries release wheel; other controls retain reduced-motion policy',()=>{
const f=fixture({reduced:true});f.next.emit('click');assert.equal(f.calls.at(-1).behavior,'instant');
f.mediaRect.top=-400;f.mediaRect.bottom=400;