horizontal
Carousel
Pure vanilla JavaScript – no frameworks, no dependencies. A
horizontal virtual list rendering 10,000 cards with
orientation: 'horizontal' and
scroll.wheel: true. Toggle
variable width to switch between fixed-size cards
and dynamic per-item widths. Only visible items are in the DOM.
Source
// Shared data and utilities for carousel example
// Supports both fixed-width and variable-width modes via a toggle
// =============================================================================
// Constants
// =============================================================================
export const ITEM_COUNT = 10_000;
export const DEFAULT_HEIGHT = 240;
export const ASPECT_RATIO = 260 / 320; // width / height ≈ 0.8125
// Color palette for cards
export const colors = [
["#667eea", "#764ba2"],
["#f093fb", "#f5576c"],
["#4facfe", "#00f2fe"],
["#43e97b", "#38f9d7"],
["#fa709a", "#fee140"],
["#a18cd1", "#fbc2eb"],
["#fccb90", "#d57eeb"],
["#e0c3fc", "#8ec5fc"],
["#f5576c", "#ff9a9e"],
["#667eea", "#00f2fe"],
];
export const emojis = [
"🎵",
"🎶",
"🎸",
"🥁",
"🎹",
"🎺",
"🎻",
"🎷",
"🪗",
"🪘",
];
export const categories = [
"Photo",
"Video",
"Music",
"Document",
"Archive",
"Code",
"Design",
"Text",
];
export const icons = ["📷", "🎬", "🎵", "📄", "📦", "💻", "🎨", "📝"];
// Width patterns — creates visual variety in variable-width mode
const widthPatterns = [120, 180, 240, 160, 200, 140, 220, 190];
// =============================================================================
// Data Generation
// =============================================================================
// Generate card items (used in both modes)
export const items = Array.from({ length: ITEM_COUNT }, (_, i) => {
const color = colors[i % colors.length];
const categoryIndex = i % categories.length;
// Variable-width fields
const baseWidth = widthPatterns[i % widthPatterns.length];
const isFeatured = i % 17 === 0;
const variableWidth = isFeatured ? baseWidth + 100 : baseWidth;
return {
id: i + 1,
title: `Card ${i + 1}`,
subtitle: `Item #${(i + 1).toLocaleString()}`,
emoji: emojis[i % emojis.length],
gradientStart: color[0],
gradientEnd: color[1],
// Variable-width extras
category: categories[categoryIndex],
icon: icons[categoryIndex],
width: variableWidth,
isFeatured,
};
});
// =============================================================================
// Templates
// =============================================================================
// Fixed-width card template
const fixedTemplate = (item) => `
<div class="card" style="background: linear-gradient(135deg, ${item.gradientStart} 0%, ${item.gradientEnd} 100%);">
<div class="card-emoji">${item.emoji}</div>
<div class="card-title">${item.title}</div>
<div class="card-subtitle">${item.subtitle}</div>
</div>
`;
// Variable-width card template
const variableTemplate = (item) => {
const featuredClass = item.isFeatured ? " card--featured" : "";
return `
<div class="card${featuredClass}" style="background: linear-gradient(135deg, ${item.gradientStart} 0%, ${item.gradientEnd} 100%);">
<div class="card-emoji">${item.icon}</div>
<div class="card-content">
<div class="card-title">${item.isFeatured ? `⭐ Featured #${item.id}` : item.title}</div>
<div class="card-category">${item.category}</div>
<div class="card-width">${item.width}px</div>
</div>
${item.isFeatured ? '<div class="card-badge">Featured</div>' : ""}
</div>
`;
};
// Returns the correct template for the current mode
export function getTemplate(variableWidth) {
return variableWidth ? variableTemplate : fixedTemplate;
}
// =============================================================================
// Config builder
// =============================================================================
// Build vlist config for the given mode, height, and gap.
// Gap is added to item dimensions so vlist spaces items correctly.
export function buildConfig(variableWidth, height = DEFAULT_HEIGHT, gap = 0) {
const cardWidth = Math.round(height * ASPECT_RATIO);
return {
orientation: "horizontal",
scroll: { wheel: true },
ariaLabel: "Horizontal card carousel",
item: {
height,
width: variableWidth
? (index) => items[index].width + gap
: cardWidth + gap,
template: getTemplate(variableWidth),
},
items,
};
}
// =============================================================================
// Utilities
// =============================================================================
// Calculate stats
export function calculateStats(domNodes, total, variableWidth) {
const saved = Math.round((1 - domNodes / total) * 100);
if (variableWidth) {
const widths = items.map((item) => item.width);
const minWidth = Math.min(...widths);
const maxWidth = Math.max(...widths);
const avgWidth = Math.round(
widths.reduce((a, b) => a + b, 0) / widths.length,
);
return { saved, minWidth, maxWidth, avgWidth };
}
return { saved };
}
// Detail panel HTML
export function getDetailHtml(item, index, variableWidth) {
return `
<div class="detail-card" style="background: linear-gradient(135deg, ${item.gradientStart} 0%, ${item.gradientEnd} 100%);">
<div class="detail-card__emoji">${variableWidth ? item.icon : item.emoji}</div>
<div class="detail-card__title">${item.title}</div>
</div>
<div class="detail-meta">
<strong>${item.title}</strong>
<span>Index ${index}${variableWidth ? ` · ${item.category} · ${item.width}px` : ` · ${item.subtitle}`}</span>
${item.isFeatured && variableWidth ? '<span class="detail-meta__badge">⭐ Featured</span>' : ""}
</div>
`;
}
// Format stats HTML
export function formatStatsHtml(domNodes, total, variableWidth) {
const stats = calculateStats(domNodes, total, variableWidth);
let html = `
<span><strong>Total:</strong> ${total.toLocaleString()} items</span>
<span><strong>DOM nodes:</strong> ${domNodes}</span>
<span><strong>Memory saved:</strong> ${stats.saved}%</span>
`;
if (variableWidth && stats.minWidth != null) {
html += `<span><strong>Width:</strong> ${stats.minWidth}–${stats.maxWidth}px (avg ${stats.avgWidth}px)</span>`;
}
return html;
}
/* Carousel Example — example-specific styles only
Common styles (.container, h1, .description, .stats, footer, .split-*, .panel-*)
are provided by example/example.css using shell.css design tokens. */
/* Center the list vertically in the split-main */
.split-main {
display: flex;
align-items: center;
justify-content: center;
height: 560px;
}
/* List container - full width, height set by JS
--card-scale is set by JS (0–1) based on slider height
--item-gap is set by JS (px) from the gap chips */
#list-container {
--card-scale: 1;
--item-gap: 8px;
--item-radius: 12px;
width: 100%;
max-width: 100%;
min-width: 0;
height: auto;
overflow: hidden;
border-radius: var(--item-radius);
}
/* Override vlist defaults for horizontal cards */
#list-container .vlist {
border: none !important;
border-radius: var(--item-radius) !important;
background: transparent;
max-width: 100%;
width: 100%;
box-sizing: border-box;
}
#list-container .vlist-viewport {
max-width: 100%;
width: 100% !important;
}
#list-container .vlist-item {
padding: 0 calc(var(--item-gap) / 2) !important;
border: none !important;
cursor: pointer;
background-color: transparent;
}
/* ============================================================================
Card
============================================================================ */
.card {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: calc(4px + 6px * var(--card-scale));
width: 100%;
height: 100%;
border-radius: var(--item-radius);
color: #fff;
text-align: center;
padding: calc(8px + 12px * var(--card-scale))
calc(6px + 8px * var(--card-scale));
position: relative;
transition: box-shadow 0.15s ease-out;
}
.vlist-item:hover .card {
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}
/* Featured cards — variable-width mode */
.card--featured {
border: 2px solid rgba(255, 255, 255, 0.3);
}
.card-emoji {
font-size: 42px;
line-height: 1;
margin-bottom: 16px;
}
.card-content {
display: flex;
flex-direction: column;
gap: 4px;
width: 100%;
}
.card-title {
font-weight: 600;
font-size: calc(11px + 5px * var(--card-scale));
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 100%;
}
.card-subtitle {
font-size: calc(10px + 3px * var(--card-scale));
opacity: 0.85;
white-space: nowrap;
}
.card-category {
font-size: calc(9px + 3px * var(--card-scale));
opacity: 0.85;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.card-width {
font-size: calc(8px + 3px * var(--card-scale));
opacity: 0.7;
font-family: monospace;
}
.card-badge {
position: absolute;
top: 6px;
right: 6px;
background: rgba(255, 255, 255, 0.25);
backdrop-filter: blur(4px);
padding: 2px 6px;
border-radius: 10px;
font-size: 8px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
line-height: 1.4;
}
/* ============================================================================
Gap chip buttons
============================================================================ */
.ctrl-btn {
padding: 6px 14px;
border: 1px solid var(--border);
border-radius: 6px;
background: var(--bg-card);
color: var(--text-muted);
font-size: 13px;
font-weight: 600;
font-family: inherit;
cursor: pointer;
transition: all 0.15s ease;
min-width: 36px;
text-align: center;
}
.ctrl-btn:hover {
border-color: var(--accent);
color: var(--accent-text);
}
.ctrl-btn--active {
background: var(--accent);
color: white;
border-color: var(--accent);
}
/* ============================================================================
Toggle switch
============================================================================ */
.toggle {
display: flex;
align-items: center;
cursor: pointer;
user-select: none;
}
.toggle input {
display: none;
}
.toggle-track {
position: relative;
width: 36px;
height: 20px;
border-radius: 10px;
background: var(--border);
transition: background 0.2s ease;
flex-shrink: 0;
}
.toggle input:checked + .toggle-track {
background: var(--accent, #667eea);
}
.toggle-track::after {
content: "";
position: absolute;
top: 2px;
left: 2px;
width: 16px;
height: 16px;
border-radius: 50%;
background: #fff;
transition: transform 0.2s ease;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}
.toggle input:checked + .toggle-track::after {
transform: translateX(16px);
}
/* ============================================================================
Card Detail (panel preview)
============================================================================ */
.detail-card {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 8px;
width: 100%;
height: 120px;
border-radius: 10px;
color: #fff;
text-align: center;
padding: 16px 12px;
margin-bottom: 8px;
}
.detail-card__emoji {
font-size: 32px;
line-height: 1;
}
.detail-card__title {
font-weight: 600;
font-size: 14px;
}
.detail-meta {
display: flex;
flex-direction: column;
gap: 2px;
font-size: 13px;
}
.detail-meta strong {
font-weight: 600;
}
.detail-meta span {
font-size: 12px;
color: var(--text-muted);
}
.detail-meta__badge {
font-size: 11px;
color: var(--accent, #667eea);
font-weight: 600;
}