Pointer Parallax Layers
Stacked layers shift by different amounts as the pointer moves, creating a sense of depth.
AI prompt
Create a 300x180px scene with four absolutely positioned layers: a large soft circle (back, depth 0.2), a medium rounded square (depth 0.5), a small circle (depth 0.8) and a text label "Depth" (depth 1), in a grey palette. On pointermove inside the scene compute the pointer position relative to the centre in the range -1 to 1 and store it as CSS custom properties --px and --py; each layer translates by calc(var(--px) * depth * 14px) and calc(var(--py) * depth * 14px). Batch updates with requestAnimationFrame and use a 200ms ease-out transition on the layers while tracking. On pointerleave set both variables to 0 with a 500ms cubic-bezier(.2,.7,.2,1) transition. Only enable it for pointer type mouse or pen and when hover is available. Under prefers-reduced-motion keep all layers static. The scene is decorative: aria-hidden on the layers except the label text.About this pattern
Definition
Several layers are translated in proportion to the pointer position, with nearer layers moving more than distant ones. The difference in speed reads as depth.
Purpose & UX principles
- Adds atmosphere to hero areas and featured objects.
- Purely decorative: it carries no information and never hides content.
- Movement is small (under 14px) so it stays comfortable; it is disabled for reduced motion and touch.
Interaction states
- Rest (centred), tracking (layers follow), leaving (eases back to centre).
Motion specification
- Layers update through CSS custom properties each animation frame; smoothing uses a 200ms transition.
- Return to centre: 500ms cubic-bezier(.2,.7,.2,1).
- Reduced motion: layers stay still.
Responsive behaviour
- Offsets are percentages of the container size, so the effect scales.
- Disabled on devices without a fine pointer.
Code
<div class="scene" id="scene"><i class="l a" style="--d:.2"></i><i class="l b" style="--d:.5"></i><i class="l c" style="--d:.8"></i><b class="l t" style="--d:1">Depth</b></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}
.scene{--px:0;--py:0;position:relative;width:min(300px,100%);height:180px;overflow:hidden;background:var(--surface);border:1px solid var(--line);border-radius:14px}
.l{position:absolute;display:block;transform:translate(calc(var(--px) * var(--d) * 14px),calc(var(--py) * var(--d) * 14px));transition:transform .5s cubic-bezier(.2,.7,.2,1)}
.scene.track .l{transition:transform .2s ease-out}
.a{width:150px;height:150px;border-radius:50%;background:#ececE8;left:20px;top:10px}
.b{width:90px;height:90px;border-radius:22px;background:#d8d8d3;right:34px;top:40px}
.c{width:36px;height:36px;border-radius:50%;background:#b4b4ae;left:60px;bottom:28px}
.t{left:50%;top:50%;margin:-14px 0 0 -30px;font-size:22px;font-weight:600;color:var(--text)}
@media (prefers-reduced-motion:reduce){.l,.scene.track .l{transition:none;transform:none}}const sc=document.getElementById('scene');let raf=0;
if(matchMedia('(hover:hover)').matches&&!matchMedia('(prefers-reduced-motion: reduce)').matches){
sc.addEventListener('pointermove',e=>{if(e.pointerType==='touch')return;const r=sc.getBoundingClientRect(),x=((e.clientX-r.left)/r.width-.5)*2,y=((e.clientY-r.top)/r.height-.5)*2;cancelAnimationFrame(raf);raf=requestAnimationFrame(()=>{sc.classList.add('track');sc.style.setProperty('--px',x.toFixed(3));sc.style.setProperty('--py',y.toFixed(3));});});
sc.addEventListener('pointerleave',()=>{cancelAnimationFrame(raf);sc.classList.remove('track');sc.style.setProperty('--px',0);sc.style.setProperty('--py',0);});
}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>Pointer Parallax Layers — 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}}
.scene{--px:0;--py:0;position:relative;width:min(300px,100%);height:180px;overflow:hidden;background:var(--surface);border:1px solid var(--line);border-radius:14px}
.l{position:absolute;display:block;transform:translate(calc(var(--px) * var(--d) * 14px),calc(var(--py) * var(--d) * 14px));transition:transform .5s cubic-bezier(.2,.7,.2,1)}
.scene.track .l{transition:transform .2s ease-out}
.a{width:150px;height:150px;border-radius:50%;background:#ececE8;left:20px;top:10px}
.b{width:90px;height:90px;border-radius:22px;background:#d8d8d3;right:34px;top:40px}
.c{width:36px;height:36px;border-radius:50%;background:#b4b4ae;left:60px;bottom:28px}
.t{left:50%;top:50%;margin:-14px 0 0 -30px;font-size:22px;font-weight:600;color:var(--text)}
@media (prefers-reduced-motion:reduce){.l,.scene.track .l{transition:none;transform:none}}
</style></head>
<body><div class="stage">
<div class="scene" id="scene"><i class="l a" style="--d:.2"></i><i class="l b" style="--d:.5"></i><i class="l c" style="--d:.8"></i><b class="l t" style="--d:1">Depth</b></div>
</div>
<script>
const sc=document.getElementById('scene');let raf=0;
if(matchMedia('(hover:hover)').matches&&!matchMedia('(prefers-reduced-motion: reduce)').matches){
sc.addEventListener('pointermove',e=>{if(e.pointerType==='touch')return;const r=sc.getBoundingClientRect(),x=((e.clientX-r.left)/r.width-.5)*2,y=((e.clientY-r.top)/r.height-.5)*2;cancelAnimationFrame(raf);raf=requestAnimationFrame(()=>{sc.classList.add('track');sc.style.setProperty('--px',x.toFixed(3));sc.style.setProperty('--py',y.toFixed(3));});});
sc.addEventListener('pointerleave',()=>{cancelAnimationFrame(raf);sc.classList.remove('track');sc.style.setProperty('--px',0);sc.style.setProperty('--py',0);});
}
</script>
</body></html>