All effectsScroll & reveal

Virtual Window Feed

虚拟窗口列表

A few visible rows are recycled while the scrollbar continues to represent hundreds of positions.

Scroll inside the example

Where to use it

Keep long dashboards or activity feeds responsive without creating a DOM node for every row. Choose it for long, often-open lists with stable row identities. Keep selection and keyboard focus tied to the data as rows are reused.

Practical example

An operations console presents thousands of timestamped job runs. Dragging the scrollbar near the bottom brings the oldest jobs into view while the list reuses the few rows currently on screen.

AI prompt

Add Virtual Window Feed to [target]. Set the rail height from row count, derive a buffered index window from scrollTop, update only visible rows, and retain accessible position and total count. Extend the window when the viewport grows. Keep the controls keyboard accessible and provide a reduced-motion state.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Virtual Window Feed</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .scroll-port {
      --frame: clamp(112px, 75vh, 242px);
      --p: 0;
      width: min(100%, 290px); height: var(--frame);
      overflow-y:auto; overflow-x:hidden; scroll-behavior:auto;
      overscroll-behavior:contain; scrollbar-width:thin;
      scrollbar-color:#a79bd5 #20223d; border-radius:12px;
      border:1px solid #a79bd567; background:#20233c; color:#f5f2ff;
    }
    .scroll-port:focus-visible { outline-offset: 3px; }
    .scroll-track { position:relative; min-height:calc(var(--frame) + 235px); }
    .scroll-pin { position:sticky; top:0; height:var(--frame); overflow:hidden; }
    .scroll-pin .tag { position:absolute; z-index:4; left:8px; bottom:9px; font-size:10px; color:#ecfcff; padding:4px 6px; background:#17152ce8; border-radius:4px; }
    .scroll-panel { min-height:62px; padding:9px; border:1px solid #adb2e345; border-radius:8px; background:linear-gradient(120deg,#314861,#3e346a); }


    .virtual-rail { position:relative; height:10200px; }
    .virtual-rail > div { position:absolute; left:6px; right:6px; height:29px; display:flex; align-items:center; justify-content:space-between; padding:0 7px; border:1px solid #b1b9f140; border-radius:5px; background:#334169; font-size:11px; }
    .virtual-rail i { width:36px; height:5px; background:linear-gradient(90deg,#af8dea,#9bdbdb); border-radius:6px; }

    @media (prefers-reduced-motion: reduce) {
      .lab, .lab *, .lab::before, .lab::after, .lab *::before, .lab *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
      }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="lab scroll-port" role="region" tabindex="0" aria-label="Scroll the virtual feed"><div class="virtual-rail" role="list"></div></div>
  </main>
  <script>
    const port = document.querySelector('.scroll-port');
    const reduced = matchMedia('(prefers-reduced-motion: reduce)');
    const listeners = [];
    let scheduled = 0;
    function sample() {
      const max = Math.max(1,port.scrollHeight - port.clientHeight);
      return Math.max(0,Math.min(1,port.scrollTop / max));
    }
    function update() {
      scheduled = 0;
      const progress = sample();
      port.style.setProperty('--p', progress.toFixed(4));
      for (const callback of listeners) callback(progress);
    }
    function schedule() { if (!scheduled) scheduled = requestAnimationFrame(update); }
    function onScroll(callback) { listeners.push(callback); callback(sample()); }
    port.addEventListener('scroll', schedule, {passive:true});
    new ResizeObserver(schedule).observe(port);
    reduced.addEventListener('change', schedule);
    update();

    const rail = port.querySelector('.virtual-rail');
    const maximum = 300;
    const size = 34;
    const entries = [];
    function makeRow() {
      const row = document.createElement('div');
      row.setAttribute('role','listitem');
      row.setAttribute('aria-setsize',String(maximum));
      row.innerHTML = '<span></span><i aria-hidden="true"></i>';
      rail.append(row);
      entries.push(row);
    }
    function showRows() {
      const count = Math.min(maximum, Math.ceil(port.clientHeight / size) + 4);
      while (entries.length < count) makeRow();
      while (entries.length > count) entries.pop().remove();
      const start = Math.max(0, Math.min(maximum - entries.length,Math.floor(port.scrollTop / size)-2));
      entries.forEach((row,index) => {
        const position = start + index;
        row.style.top = position * size + 'px';
        row.setAttribute('aria-posinset',String(position + 1));
        row.firstElementChild.textContent = (document.documentElement.lang === 'zh' ? '记录 ' : 'Record ') + (position + 1);
      });
    }
    onScroll(showRows);
  </script>
</body>
</html>

Original example by UI Skills

Further examples