Directional Page Slide
Views slide left when moving forward and right when moving back, so direction is felt.
AI prompt
Build a three-step flow (Plan, Build, Ship) inside a 300x170px viewport with overflow hidden and Previous and Next buttons below. Each step is a panel with a heading and a short paragraph. On Next the current panel animates translateX 0 to -24% and fades out while the new panel animates from translateX 24% and opacity 0 to rest; on Previous the directions are mirrored. Use the Web Animations API with 320ms cubic-bezier(.2,.7,.2,1); the new panel is placed in the DOM immediately and the old one is removed on finish. Disable both buttons while animating and disable Previous at the first step and Next at the last. Show the step as "Step 2 of 3" in an aria-live region and move no focus. Under prefers-reduced-motion use a 100ms cross-fade without movement.About this pattern
Definition
When changing views, the outgoing view slides out in one direction while the incoming one slides in from the other. Moving forward and moving back use opposite directions.
Purpose & UX principles
- Reinforces the app's spatial model: forward goes right, back comes from the left.
- Short, subtle slides (about 24% of the width) feel faster than full-width sweeps and reduce motion for sensitive users.
- The view content changes immediately for assistive technology; the animation is purely visual.
Interaction states
- Idle, transitioning (buttons briefly disabled), first and last (the matching button disabled).
Motion specification
- Outgoing: translateX 0 to -24% (or +24%) with opacity 1 to 0; incoming mirrors it; 320ms cubic-bezier(.2,.7,.2,1).
- Reduced motion: a 100ms cross-fade with no movement.
Responsive behaviour
- The slide distance is a percentage of the container, so it scales with width.
- Previous and Next buttons stay fixed below the viewport region.
Code
<div class="box">
<div class="vp" id="vp"></div>
<div class="nav"><button id="pv">Previous</button><span id="st" aria-live="polite"></span><button id="nx">Next</button></div>
</div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.box{width:min(300px,100%);display:grid;gap:10px}
.vp{position:relative;height:130px;overflow:hidden;background:var(--surface);border:1px solid var(--line);border-radius:14px}
.pn{position:absolute;inset:0;padding:18px}
.pn b{display:block;font-size:17px;font-weight:600;margin-bottom:6px}
.pn p{font-size:13px;line-height:1.5;color:var(--muted)}
.nav{display:flex;align-items:center;justify-content:space-between}
.nav button{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12.5px;cursor:pointer}
.nav button:disabled{opacity:.35;cursor:default}
.nav button:hover:not(:disabled){border-color:var(--accent)}
.nav button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
#st{font-size:12px;color:var(--muted)}const steps=[['Plan','Sketch the flow and decide what matters most.'],['Build','Make the smallest version that works end to end.'],['Ship','Release, watch real use and refine the details.']];
const vp=document.getElementById('vp'),pv=document.getElementById('pv'),nx=document.getElementById('nx'),st=document.getElementById('st');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;let i=0,busy=false;
const make=n=>{const d=document.createElement('div');d.className='pn';d.innerHTML='<b>'+steps[n][0]+'</b><p>'+steps[n][1]+'</p>';return d;};
function btn(){pv.disabled=busy||i===0;nx.disabled=busy||i===steps.length-1;st.textContent='Step '+(i+1)+' of '+steps.length;}
vp.append(make(0));btn();
function go(dir){if(busy)return;const old=vp.firstElementChild;i+=dir;const nw=make(i);vp.append(nw);busy=true;btn();
const ease='cubic-bezier(.2,.7,.2,1)',d=reduce?100:320;
const a=reduce?[{opacity:1},{opacity:0}]:[{transform:'none',opacity:1},{transform:'translateX('+(-24*dir)+'%)',opacity:0}];
const b=reduce?[{opacity:0},{opacity:1}]:[{transform:'translateX('+(24*dir)+'%)',opacity:0},{transform:'none',opacity:1}];
old.animate(a,{duration:d,easing:ease,fill:'forwards'});
nw.animate(b,{duration:d,easing:ease}).finished.then(()=>{old.remove();busy=false;btn();});}
nx.addEventListener('click',()=>go(1));pv.addEventListener('click',()=>go(-1));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Directional Page Slide — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.box{width:min(300px,100%);display:grid;gap:10px}
.vp{position:relative;height:130px;overflow:hidden;background:var(--surface);border:1px solid var(--line);border-radius:14px}
.pn{position:absolute;inset:0;padding:18px}
.pn b{display:block;font-size:17px;font-weight:600;margin-bottom:6px}
.pn p{font-size:13px;line-height:1.5;color:var(--muted)}
.nav{display:flex;align-items:center;justify-content:space-between}
.nav button{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12.5px;cursor:pointer}
.nav button:disabled{opacity:.35;cursor:default}
.nav button:hover:not(:disabled){border-color:var(--accent)}
.nav button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
#st{font-size:12px;color:var(--muted)}
</style></head>
<body><div class="stage">
<div class="box">
<div class="vp" id="vp"></div>
<div class="nav"><button id="pv">Previous</button><span id="st" aria-live="polite"></span><button id="nx">Next</button></div>
</div>
</div>
<script>
const steps=[['Plan','Sketch the flow and decide what matters most.'],['Build','Make the smallest version that works end to end.'],['Ship','Release, watch real use and refine the details.']];
const vp=document.getElementById('vp'),pv=document.getElementById('pv'),nx=document.getElementById('nx'),st=document.getElementById('st');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;let i=0,busy=false;
const make=n=>{const d=document.createElement('div');d.className='pn';d.innerHTML='<b>'+steps[n][0]+'</b><p>'+steps[n][1]+'</p>';return d;};
function btn(){pv.disabled=busy||i===0;nx.disabled=busy||i===steps.length-1;st.textContent='Step '+(i+1)+' of '+steps.length;}
vp.append(make(0));btn();
function go(dir){if(busy)return;const old=vp.firstElementChild;i+=dir;const nw=make(i);vp.append(nw);busy=true;btn();
const ease='cubic-bezier(.2,.7,.2,1)',d=reduce?100:320;
const a=reduce?[{opacity:1},{opacity:0}]:[{transform:'none',opacity:1},{transform:'translateX('+(-24*dir)+'%)',opacity:0}];
const b=reduce?[{opacity:0},{opacity:1}]:[{transform:'translateX('+(24*dir)+'%)',opacity:0},{transform:'none',opacity:1}];
old.animate(a,{duration:d,easing:ease,fill:'forwards'});
nw.animate(b,{duration:d,easing:ease}).finished.then(()=>{old.remove();busy=false;btn();});}
nx.addEventListener('click',()=>go(1));pv.addEventListener('click',()=>go(-1));
</script>
</body></html>