Profile pages on short-video sites are long grids of 9:16 thumbnails, often hundreds of them. Rendering every image and preview at once makes the page slow, especially on phones. This post builds a small vertical-video grid in plain JavaScript that loads thumbnails only when they approach the viewport, plays a muted preview on hover, and keeps layout stable while images load.
Markup and layout
<ul class="grid" id="grid"></ul>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 8px;
list-style: none;
padding: 0;
}
.tile {
position: relative;
aspect-ratio: 9 / 16; /* reserves space, no layout shift */
background: #222;
border-radius: 8px;
overflow: hidden;
}
.tile img, .tile video {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.tile .views {
position: absolute;
left: 6px;
bottom: 6px;
color: #fff;
font: 600 12px/1 system-ui, sans-serif;
text-shadow: 0 1px 2px rgba(0, 0, 0, .6);
}
aspect-ratio: 9 / 16 is the important line. Each tile has its final size before the image arrives, so the grid never jumps.
Creating tiles without loading images
Store the real URL in a data attribute and leave src empty:
function createTile({ thumb, preview, views }) {
const li = document.createElement("li");
li.className = "tile";
li.innerHTML = `
<img alt="" data-src="${thumb}" decoding="async">
<span class="views">${views}</span>`;
li.dataset.preview = preview || "";
return li;
}
Lazy loading with IntersectionObserver
const io = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
const img = entry.target.querySelector("img[data-src]");
if (img) {
img.src = img.dataset.src;
img.removeAttribute("data-src");
}
io.unobserve(entry.target);
}
}, { rootMargin: "400px 0px" }); // start loading a bit before tiles appear
A rootMargin of a few hundred pixels hides the loading from users who scroll at a normal speed. Native loading="lazy" also works for images, but the observer gives you one place to hook in previews, analytics or placeholders.
Hover previews that do not pile up
Creating a <video> element for every tile is expensive. Create one on hover and remove it on leave:
function attachPreview(tile) {
let video = null;
tile.addEventListener("mouseenter", () => {
if (!tile.dataset.preview) return;
video = document.createElement("video");
Object.assign(video, { src: tile.dataset.preview, muted: true, loop: true, playsInline: true });
tile.appendChild(video);
video.play().catch(() => {}); // autoplay can still be blocked
});
tile.addEventListener("mouseleave", () => {
if (video) { video.pause(); video.remove(); video = null; }
});
}
Muted, inline playback is what browsers allow without a user gesture. On touch devices skip previews entirely; matchMedia("(hover: hover)") tells you whether hover exists.
Rendering in batches
const grid = document.getElementById("grid");
const canHover = matchMedia("(hover: hover)").matches;
function render(items) {
const frag = document.createDocumentFragment();
for (const item of items) {
const tile = createTile(item);
if (canHover) attachPreview(tile);
io.observe(tile);
frag.appendChild(tile);
}
grid.appendChild(frag);
}
For very long lists, fetch the next page when a sentinel element at the end of the grid intersects, and call render again.
Seeing the pattern in practice
If you want to see how a real public profile grid behaves, with hundreds of tiles and view counts on each, a public TikTok profile viewer shows that layout in a normal browser tab. Watching the network panel while scrolling one is a quick way to compare a production grid with your own implementation.
Summary
- Reserve space with
aspect-ratioto avoid layout shift. - Load thumbnails with IntersectionObserver and a generous
rootMargin. - Create preview videos on demand and destroy them on leave.
- Append tiles in batches with a DocumentFragment.
Top comments (0)