Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
1500 lines (1325 loc) · 55.4 KB
/
Copy pathindex.html
File metadata and controls
1500 lines (1325 loc) · 55.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hcg-virtual-scroll Demo - JavaScript Virtual Scrolling Library</title>
<meta name="description" content="Live demos of hcg-virtual-scroll, a high-performance vanilla JavaScript virtual scrolling library. Scroll 1,000,000+ rows smoothly with only the visible rows in the DOM - fixed and dynamic heights, tables, infinite scroll, and chat mode.">
<link rel="canonical" href="https://www.html-code-generator.com/demo/javascript/virtual-scrolling.html">
<!-- Open Graph -->
<meta property="og:type" content="website">
<meta property="og:title" content="JavaScript Virtual Scrolling - Live Demos | hcg-virtual-scroll">
<meta property="og:description" content="Live demos of hcg-virtual-scroll, a high-performance vanilla JavaScript virtual scrolling library. Scroll 1,000,000+ rows smoothly with only the visible rows in the DOM.">
<meta property="og:url" content="https://www.html-code-generator.com/demo/javascript/virtual-scrolling.html">
<meta property="og:image" content="https://www.html-code-generator.com/images/javascript/198/og/virtual-scrolling.webp">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:site_name" content="HTML Code Generator">
<!-- Twitter Card -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="JavaScript Virtual Scrolling - Live Demos | hcg-virtual-scroll">
<meta name="twitter:description" content="Scroll 1,000,000+ rows smoothly with only the visible rows in the DOM. Live demos of the vanilla JavaScript virtual scrolling library.">
<meta name="twitter:image" content="https://www.html-code-generator.com/images/javascript/198/og/virtual-scrolling.webp">
<link rel="stylesheet" href="hcg-virtual-scroll.css">
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
background: #fff;
color: #1f2430;
padding: 48px 24px 64px;
min-height: 100vh;
}
.wrap { max-width: 960px; margin: 0 auto; }
h1 {
font-size: 1.65rem;
font-weight: 700;
color: #111827;
margin-bottom: 10px;
line-height: 1.3;
}
.subtitle {
font-size: .95rem;
color: #4b5563;
line-height: 1.7;
margin-bottom: 36px;
}
.subtitle code {
background: #f3f4f6;
border-radius: 4px;
padding: 1px 6px;
font-size: .82rem;
color: #4338ca;
}
.subtitle a {
color: #4338ca;
font-weight: 600;
text-decoration: none;
}
.subtitle a:hover { text-decoration: underline; }
.demo-section { margin-bottom: 32px; scroll-margin-top: 16px; }
.demo-section > h2 {
font-size: 1.05rem;
font-weight: 700;
color: #111827;
margin-bottom: 6px;
}
.demo-desc {
font-size: .86rem;
color: #6b7280;
line-height: 1.6;
margin-bottom: 14px;
}
.panel {
background: #fff;
border: 1px solid #e5e7eb;
border-radius: 14px;
padding: 20px;
margin-bottom: 12px;
}
.panel-label {
display: block;
font-size: .72rem;
letter-spacing: .08em;
text-transform: uppercase;
color: #9ca3af;
font-weight: 600;
margin-bottom: 12px;
}
.demo-meta { font-size: .76rem; color: #9ca3af; margin-bottom: 12px; }
.stats-bar {
display: flex;
gap: 16px;
flex-wrap: wrap;
font-size: .76rem;
color: #6b7280;
margin-bottom: 12px;
padding: 8px 12px;
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 8px;
}
.stats-bar span { white-space: nowrap; }
.stats-bar b { color: #111827; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }
.btn-row input[type=number],
.btn-row input[type=text] {
padding: 8px 12px;
border: 1px solid #d1d5db;
border-radius: 8px;
font-size: .82rem;
color: #374151;
}
.btn-row input[type=number] { width: 90px; }
.demo-btn {
background: #fff;
border: 1px solid #d1d5db;
border-radius: 8px;
padding: 8px 16px;
color: #4b5563;
font-size: .82rem;
font-weight: 600;
cursor: pointer;
transition: border-color .15s, color .15s, background .15s;
}
.demo-btn:hover { border-color: #9ca3af; color: #1f2937; background: #f9fafb; }
.demo-btn--primary { border-color: #c7d2fe; color: #4338ca; }
.demo-btn--primary:hover { background: #eef2ff; border-color: #a5b4fc; }
.demo-btn--danger { border-color: #fecaca; color: #b91c1c; }
.demo-btn--danger:hover { background: #fef2f2; border-color: #fca5a5; }
.demo-code {
margin-top: 4px;
border: 1px solid #e5e7eb;
border-radius: 10px;
background: #f9fafb;
overflow: hidden;
}
.demo-code summary {
padding: 10px 14px;
font-size: .82rem;
font-weight: 600;
color: #4338ca;
cursor: pointer;
user-select: none;
list-style: none;
}
.demo-code summary::-webkit-details-marker { display: none; }
.demo-code summary::before {
content: '▸ ';
display: inline-block;
transition: transform .15s;
}
.demo-code[open] summary::before { transform: rotate(90deg); }
.demo-code pre {
margin: 0;
padding: 0 14px 14px;
overflow-x: auto;
}
.demo-code-label {
margin: 0;
padding: 8px 14px 4px;
font-size: .68rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: .06em;
color: #6b7280;
}
.demo-code-label + .demo-code-label,
.demo-code pre + .demo-code-label {
border-top: 1px solid #e5e7eb;
margin-top: 4px;
padding-top: 12px;
}
.demo-code code {
display: block;
font-family: "Fira Code", "Cascadia Code", ui-monospace, monospace;
font-size: .76rem;
line-height: 1.65;
color: #374151;
white-space: pre;
}
/* -- Scroll list container -- */
.scroll-list { height: 360px; border: 1px solid #e5e7eb; border-radius: 8px; background: #fafafa; }
/* -- Basic item -- */
.hcg-vs-item-basic {
display: flex; align-items: center; padding: 0 16px; box-sizing: border-box;
border-bottom: 1px solid #f0f0f0; font-size: 0.875rem; background: #fff; transition: background .1s;
}
.hcg-vs-item-basic:hover { background: #f0f7ff; }
.hcg-vs-item-basic .idx { width: 60px; font-variant-numeric: tabular-nums; color: #999; font-size: 0.75rem; }
.hcg-vs-item-basic .name { flex: 1; font-weight: 500; }
.hcg-vs-item-basic .badge { font-size: 0.7rem; padding: 2px 8px; border-radius: 99px; font-weight: 600; }
.badge-green { background: #dcfce7; color: #16a34a; }
.badge-blue { background: #dbeafe; color: #1d4ed8; }
.badge-orange { background: #ffedd5; color: #c2410c; }
.badge-purple { background: #f3e8ff; color: #7e22ce; }
/* -- Dynamic-height items -- */
.hcg-vs-item-dynamic {
padding: 8px 16px; border-bottom: 1px solid #f0f0f0; background: #fff;
display: flex; flex-direction: column; justify-content: center; box-sizing: border-box; overflow: hidden;
}
.hcg-vs-item-dynamic:hover { background: #fafafa; }
.hcg-vs-item-dynamic .title { font-weight: 600; font-size: 0.875rem; }
.hcg-vs-item-dynamic .body { font-size: 0.78rem; color: #666; margin-top: 3px; line-height: 1.4; }
/* -- Card items -- */
.hcg-vs-card {
margin: 6px 10px; padding: 12px 16px; border: 1px solid #e5e7eb; border-radius: 8px;
background: #fff; box-sizing: border-box; display: flex; align-items: center; gap: 14px; transition: box-shadow .15s;
}
.hcg-vs-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,.1); }
.hcg-vs-card .avatar {
width: 44px; height: 44px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
font-weight: 700; font-size: 1rem; flex-shrink: 0; color: #fff;
}
.hcg-vs-card .info { flex: 1; min-width: 0; }
.hcg-vs-card .info .cname { font-weight: 600; font-size: 0.9rem; }
.hcg-vs-card .info .csub { font-size: 0.78rem; color: #888; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hcg-vs-card .score { font-size: 1.1rem; font-weight: 700; color: #2563eb; }
.tag {
display: inline-block; font-size: .65rem; font-weight: 600; padding: 2px 7px; border-radius: 99px;
background: #eef2ff; color: #4338ca; vertical-align: middle; margin-left: 4px; letter-spacing: .3px;
}
/* -- Recycling items (Demo 3) -- */
.hcg-vs-item-check {
display: flex; align-items: center; gap: 10px; padding: 0 14px;
border-bottom: 1px solid #f0f0f0; background: #fff; box-sizing: border-box;
}
.hcg-vs-item-check:hover { background: #fafafa; }
.hcg-vs-item-check .item-cb { width: 16px; height: 16px; flex-shrink: 0; cursor: pointer; accent-color: #2563eb; }
.avatar-sm {
width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
font-size: 0.75rem; font-weight: 700; color: #fff; flex-shrink: 0;
}
.check-info { flex: 1; min-width: 0; }
.check-name { font-weight: 600; font-size: 0.85rem; }
.check-sub { font-size: 0.74rem; color: #888; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* -- Infinite scroll loading badge -- */
.loading-badge {
display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 2px 10px; border-radius: 99px;
background: #fef9c3; color: #854d0e; animation: pulse .9s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .5; } }
/* -- Chat list (Demo 6) -- */
.chat-list { background: #f1f5f9; }
.chat-row { display: flex; padding: 4px 12px; box-sizing: border-box; }
.chat-row.me { justify-content: flex-end; }
.chat-row.bot { justify-content: flex-start; }
.chat-bubble {
max-width: 72%; padding: 8px 12px; border-radius: 14px; font-size: 0.85rem; line-height: 1.4; word-break: break-word;
}
.bubble-me { background: #2563eb; color: #fff; border-bottom-right-radius: 4px; }
.bubble-bot { background: #fff; color: #222; border: 1px solid #e2e8f0; border-bottom-left-radius: 4px; }
.bubble-text { margin-bottom: 2px; }
.bubble-time { font-size: 0.68rem; opacity: .65; text-align: right; }
/* -- Empty state -- */
.hcg-vs-empty {
display: flex; align-items: center; justify-content: center; height: 100%;
color: #bbb; font-size: 0.9rem; pointer-events: none;
}
/* -- Virtual table demo (section 8) -- */
.vs-table-wrap {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
border: 1px solid #e5e7eb;
border-radius: 8px;
}
.vs-table-inner {
min-width: 520px;
}
#s8-header {
width: 100%;
box-sizing: border-box;
background: #f9fafb;
border-bottom: 2px solid #e5e7eb;
}
#s8-header table,
#list8 .hcg-vs-item > table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
box-sizing: border-box;
}
#list8 {
box-sizing: border-box;
}
#s8-header th,
#list8 .hcg-vs-item td {
padding: 10px 14px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
border-bottom: none;
}
#s8-header th.col-id,
#list8 .hcg-vs-item td.col-id { width: 80px; }
#list8 .hcg-vs-item td.col-id { color: #999; }
#s8-header th.col-status,
#list8 .hcg-vs-item td.col-status { width: 110px; }
#list8 .hcg-vs-item td.col-status { font-weight: 600; }
@media (max-width: 600px) {
.vs-table-inner { min-width: 480px; }
}
</style>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "WebPage",
"name": "hcg-virtual-scroll Demo - JavaScript Virtual Scrolling Library",
"description": "Live demos of hcg-virtual-scroll: fixed and dynamic row heights, DOM recycling, infinite scroll, chat reverse mode, virtual tables, and live search filtering.",
"url": "https://www.html-code-generator.com/demo/javascript/virtual-scrolling.html",
"inLanguage": "en",
"isPartOf": {
"@type": "WebSite",
"name": "HTML Code Generator",
"url": "https://www.html-code-generator.com"
}
}
</script>
</head>
<body>
<div class="wrap">
<h1>hcg-virtual-scroll - Live Demo</h1>
<p class="subtitle">
Interactive demos of <code>hcg-virtual-scroll</code>, a high-performance zero-dependency JavaScript
virtual scrolling library. Scroll 1,000,000+ rows smoothly with only the visible rows in the DOM -
try fixed and dynamic heights, DOM recycling, infinite scroll, chat mode, tables, and live search.
Open DevTools and watch the DOM node count stay small while you scroll.
<a href="https://www.html-code-generator.com/javascript/virtual-scrolling">Full documentation</a>
</p>
<!-- =
DEMO 1 - Fixed height, 10 000 rows
= -->
<section class="demo-section" id="demo-fixed-height">
<h2>1. Fixed height list - 10,000 rows</h2>
<p class="demo-desc">The most common virtual scrolling setup. Every row shares the same height, so the library calculates positions instantly. Only visible rows stay in the DOM.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">10 000 rows · itemHeight: 50 · bufferSize: 3 · adaptiveOverscan: true</p>
<div class="stats-bar">
<span>Scroll: <b id="s1-scroll">0</b>px</span>
<span>Visible: <b id="s1-start">-</b> - <b id="s1-end">-</b></span>
<span>DOM nodes: <b id="s1-dom">-</b></span>
</div>
<div class="btn-row">
<input type="number" id="s1-idx" placeholder="Index" min="0" max="9999">
<button class="demo-btn demo-btn--primary" type="button" onclick="s1ScrollTo()">scrollTo(index)</button>
<button class="demo-btn" type="button" onclick="s1Top()">scrollToTop()</button>
<button class="demo-btn" type="button" onclick="s1Bottom()">scrollToBottom()</button>
<button class="demo-btn" type="button" onclick="s1Range()">getVisibleRange()</button>
</div>
<div id="list1" class="scroll-list"></div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<div id="list1" style="height:360px;overflow-y:auto"></div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>const vs = new HCGVirtualScroll(data, {
container: '#list1',
itemHeight: 50,
bufferSize: 3,
adaptiveOverscan: true,
renderItem(item, i) {
return `<div class="row">
<span>#${i}</span>
<span>${item.name}</span>
<span class="badge">${item.badge}</span>
</div>`;
},
onScroll(scrollTop, { start, end }) {
console.log('scroll:', scrollTop, 'visible:', start, '-', end);
},
});</code></pre>
</details>
</section>
<!-- =
DEMO 2 - Dynamic height, 5 000 rows
= -->
<section class="demo-section" id="demo-dynamic-height">
<h2>2. Dynamic row heights</h2>
<p class="demo-desc">When rows vary in height, pass a function to <code>itemHeight</code>. Each item's height is pre-calculated so variable-height rows position correctly without measuring the DOM during scroll.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">5 000 rows · itemHeight: function · heights 44 - 96 px</p>
<div class="stats-bar">
<span>Scroll: <b id="s2-scroll">0</b>px</span>
<span>Visible: <b id="s2-start">-</b> - <b id="s2-end">-</b></span>
<span>DOM nodes: <b id="s2-dom">-</b></span>
</div>
<div class="btn-row">
<input type="number" id="s2-idx" placeholder="Index" min="0" max="4999">
<button class="demo-btn demo-btn--primary" type="button" onclick="s2ScrollTo()">scrollTo(index)</button>
<button class="demo-btn" type="button" onclick="s2Top()">scrollToTop()</button>
<button class="demo-btn" type="button" onclick="s2Bottom()">scrollToBottom()</button>
<button class="demo-btn" type="button" onclick="s2Update()">updateData (reload)</button>
<button class="demo-btn demo-btn--danger" type="button" onclick="s2Clear()">clear()</button>
</div>
<div id="list2" class="scroll-list"></div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<div id="list2" style="height:360px;overflow-y:auto"></div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>const data = items.map(item => ({
...item,
height: item.body.length > 80 ? 96 : 44,
}));
const vs = new HCGVirtualScroll(data, {
container: '#list2',
itemHeight: item => item.height,
estimatedItemHeight: 60,
bufferSize: 4,
renderItem(item) {
return `<div style="height:${item.height}px">
<strong>${item.title}</strong>
<p>${item.body}</p>
</div>`;
},
});</code></pre>
</details>
</section>
<!-- =
DEMO 3 - DOM Recycling with keyField
= -->
<section class="demo-section" id="demo-dom-recycling">
<h2>3. DOM recycling with keyField <span class="tag">keyField</span></h2>
<p class="demo-desc">Set <code>keyField</code> to a unique property and the library reuses existing DOM nodes instead of recreating them. Checkbox state survives scrolling away and back.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">1 000 cards · keyField: "id" · tick boxes, scroll away, scroll back - state is kept</p>
<div class="stats-bar">
<span>Scroll: <b id="s3-scroll">0</b>px</span>
<span>Visible: <b id="s3-start">-</b> - <b id="s3-end">-</b></span>
<span>DOM nodes: <b id="s3-dom">-</b></span>
<span>Checked: <b id="s3-checked">0</b></span>
</div>
<div class="btn-row">
<input type="number" id="s3-idx" placeholder="Index" min="0" max="999">
<button class="demo-btn demo-btn--primary" type="button" onclick="s3ScrollTo()">scrollTo(index)</button>
<button class="demo-btn" type="button" onclick="s3Top()">scrollToTop()</button>
<button class="demo-btn" type="button" onclick="s3Bottom()">scrollToBottom()</button>
<button class="demo-btn" type="button" onclick="s3CountChecked()">count checked</button>
<button class="demo-btn demo-btn--danger" type="button" onclick="s3UncheckAll()">uncheck all</button>
</div>
<div id="list3" class="scroll-list"></div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<div id="list3" style="height:360px;overflow-y:auto"></div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>const vs = new HCGVirtualScroll(data, {
container: '#list3',
itemHeight: 72,
keyField: 'id',
renderItem(item) {
return `<div class="row">
<input type="checkbox" ${item.checked ? 'checked' : ''} class="item-cb" />
<span>${item.name}</span>
</div>`;
},
});
document.getElementById('list3').addEventListener('change', function (e) {
if (!e.target.classList.contains('item-cb')) return;
const row = e.target.closest('[data-vs-key]');
if (!row) return;
const id = parseInt(row.dataset.vsKey, 10);
data[id].checked = e.target.checked;
});</code></pre>
</details>
</section>
<!-- =
DEMO 4 - updateConfig() live hot-swap
= -->
<section class="demo-section" id="demo-updateconfig">
<h2>4. Change options at runtime with updateConfig()</h2>
<p class="demo-desc">Update row height, render function, or buffer size without destroying the instance. The list re-renders instantly with the new settings.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">2 000 rows · change itemHeight / renderItem without re-creating the instance</p>
<div class="stats-bar">
<span>Scroll: <b id="s4-scroll">0</b>px</span>
<span>Visible: <b id="s4-start">-</b> - <b id="s4-end">-</b></span>
<span>DOM nodes: <b id="s4-dom">-</b></span>
</div>
<div class="btn-row">
<button class="demo-btn" type="button" onclick="s4Compact()">itemHeight → 36</button>
<button class="demo-btn" type="button" onclick="s4Normal()">itemHeight → 60</button>
<button class="demo-btn" type="button" onclick="s4Large()">itemHeight → 90</button>
<button class="demo-btn demo-btn--primary" type="button" onclick="s4ToggleRender()">toggle renderItem</button>
<button class="demo-btn" type="button" onclick="s4Refresh()">refresh()</button>
</div>
<div id="list4" class="scroll-list"></div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<div id="list4" style="height:360px;overflow-y:auto"></div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>vs.updateConfig({ itemHeight: 36 });
vs.updateConfig({ renderItem: renderCompact });
vs.updateConfig({ bufferSize: 8, adaptiveOverscan: false });</code></pre>
</details>
</section>
<!-- =
DEMO 5 - Infinite Scroll (onReachEnd + append)
= -->
<section class="demo-section" id="demo-infinite-scroll">
<h2>5. Infinite scroll - load more on demand <span class="tag">onReachEnd · append()</span></h2>
<p class="demo-desc">Use <code>onReachEnd</code> to fetch and append more items as the user nears the bottom. This demo simulates a 600 ms network request and loads 30 rows per batch.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">Starts with 50 items · loads 30 more when near the end · simulated 600 ms delay</p>
<div class="stats-bar">
<span>Scroll: <b id="s5-scroll">0</b>px</span>
<span>Visible: <b id="s5-start">-</b> - <b id="s5-end">-</b></span>
<span>Total items: <b id="s5-total">50</b></span>
<span>Batches: <b id="s5-batches">0</b></span>
<span id="s5-loading" class="loading-badge" style="display:none">Loading…</span>
</div>
<div class="btn-row">
<button class="demo-btn demo-btn--primary" type="button" onclick="s5AppendNow()">append 30 now</button>
<button class="demo-btn" type="button" onclick="s5Top()">scrollToTop()</button>
<button class="demo-btn" type="button" onclick="s5Bottom()">scrollToBottom()</button>
<button class="demo-btn demo-btn--danger" type="button" onclick="s5Reset()">reset</button>
</div>
<div id="list5" class="scroll-list"></div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<div id="list5" style="height:360px;overflow-y:auto"></div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>const vs = new HCGVirtualScroll(initialData, {
container: '#list5',
itemHeight: 56,
reachEndThreshold: 8,
renderItem: (item, i) => `<div class="row">#${i} ${item.name}</div>`,
onReachEnd({ total }) {
if (loading) return;
loading = true;
fetchNextPage(total).then(batch => {
vs.append(batch);
loading = false;
});
},
});</code></pre>
</details>
</section>
<section class="demo-section" id="demo-chat">
<h2>6. Chat / reverse mode <span class="tag">reverse · prepend() · onReachStart</span></h2>
<p class="demo-desc">Set <code>reverse: true</code> to anchor the list to the bottom like a chat window. New messages appear at the bottom; scrolling to the top loads older history with <code>prepend()</code>.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">reverse: true · anchored to bottom · scroll to top to load older messages</p>
<div class="stats-bar">
<span>Messages: <b id="s6-total">0</b></span>
<span>Visible: <b id="s6-start">-</b> - <b id="s6-end">-</b></span>
<span id="s6-loading" class="loading-badge" style="display:none">Loading history…</span>
</div>
<div class="btn-row">
<input type="text" id="s6-input" placeholder="Type a message…" style="flex:1">
<button class="demo-btn demo-btn--primary" type="button" onclick="s6Send()">Send</button>
<button class="demo-btn" type="button" onclick="s6BotReply()">Bot reply</button>
<button class="demo-btn" type="button" onclick="s6LoadHistory()">load history</button>
</div>
<div id="list6" class="scroll-list chat-list"></div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<input type="text" id="chat-input" placeholder="Type a message…">
<div id="list6" style="height:360px;overflow-y:auto"></div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>const vs = new HCGVirtualScroll(messages, {
container: '#list6',
itemHeight: msg => msg.height,
estimatedItemHeight: 74,
reverse: true,
renderItem(msg) {
const isMe = msg.author === 'me';
return `<div class="chat-row ${isMe ? 'me' : 'bot'}">
<div class="bubble">${msg.text}</div>
</div>`;
},
onReachStart() {
loadHistory().then(older => vs.prepend(older));
},
});
vs.append([{ id: Date.now(), author: 'me', text: 'Hello!', height: 54 }]);</code></pre>
</details>
</section>
<!-- =
DEMO 7 - onReachEnd Trigger Inspector
= -->
<section class="demo-section" id="demo-onreachend">
<h2>7. onReachEnd trigger inspector <span class="tag">onReachEnd</span></h2>
<p class="demo-desc">See exactly when the <code>onReachEnd</code> callback fires. Adjust the threshold and watch the event log record each trigger as you scroll toward the end.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">100 items · scroll near the bottom to fire the trigger · watch the event log</p>
<div class="stats-bar">
<span>Scroll: <b id="s7-scroll">0</b>px</span>
<span>Visible: <b id="s7-start">-</b> - <b id="s7-end">-</b></span>
<span>Total: <b id="s7-total">100</b></span>
<span>Threshold: <b id="s7-thresh">5</b> items</span>
<span>Triggers: <b id="s7-count">0</b></span>
</div>
<div class="btn-row">
<button class="demo-btn" type="button" onclick="s7SetThresh(3)">threshold → 3</button>
<button class="demo-btn" type="button" onclick="s7SetThresh(5)">threshold → 5</button>
<button class="demo-btn" type="button" onclick="s7SetThresh(15)">threshold → 15</button>
<button class="demo-btn" type="button" onclick="s7Bottom()">scrollToBottom()</button>
<button class="demo-btn" type="button" onclick="s7Top()">scrollToTop()</button>
<button class="demo-btn demo-btn--danger" type="button" onclick="s7Reset()">reset</button>
<button class="demo-btn" type="button" onclick="s7ClearLog()">clear log</button>
</div>
<div style="display:flex;gap:12px">
<div id="list7" class="scroll-list" style="flex:1"></div>
<div id="s7-log" style="width:280px;height:360px;overflow-y:auto;border:1px solid #e5e7eb;border-radius:8px;background:#f9fafb;padding:8px;font-size:0.75rem;font-family:monospace;color:#374151">
<div style="color:#9ca3af;text-align:center;margin-top:120px">Scroll near the bottom<br>to fire onReachEnd</div>
</div>
</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<div id="list7" style="height:360px;overflow-y:auto"></div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>const vs = new HCGVirtualScroll(data, {
container: '#list7',
itemHeight: 50,
reachEndThreshold: 5,
onReachEnd({ start, end, total }) {
console.log('Reached end', { start, end, total });
},
});
vs.updateConfig({ reachEndThreshold: 15 });</code></pre>
</details>
</section>
<!-- =
DEMO 8 - TABLE rendering
= -->
<section class="demo-section" id="demo-table">
<h2>8. Virtual scrolling table - 100,000 rows <span class="tag">TABLE container</span></h2>
<p class="demo-desc">Render a large data table with a sticky header and aligned columns. Each row is a mini-table sharing the header's fixed column widths so only visible rows are rendered.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">100 000 rows · sticky header · column widths kept in sync with table-layout: fixed</p>
<div class="stats-bar">
<span>Scroll: <b id="s8-scroll">0</b>px</span>
<span>Visible: <b id="s8-start">-</b> - <b id="s8-end">-</b></span>
<span>DOM nodes: <b id="s8-dom">-</b></span>
<span>Total rows: <b id="s8-total">100000</b></span>
</div>
<div class="btn-row">
<input type="number" id="s8-idx" placeholder="Row index" min="0" max="99999">
<button class="demo-btn demo-btn--primary" type="button" onclick="s8ScrollTo()">scrollTo(index)</button>
<button class="demo-btn" type="button" onclick="s8Top()">scrollToTop()</button>
<button class="demo-btn" type="button" onclick="s8Bottom()">scrollToBottom()</button>
</div>
<div class="vs-table-wrap">
<div class="vs-table-inner">
<div id="s8-header">
<table>
<thead>
<tr>
<th class="col-id" style="padding:10px 14px;text-align:left">ID</th>
<th style="padding:10px 14px;text-align:left">Name</th>
<th style="padding:10px 14px;text-align:left">Email</th>
<th class="col-status" style="padding:10px 14px;text-align:left">Status</th>
</tr>
</thead>
</table>
</div>
<div id="list8" class="scroll-list" style="border:none;border-radius:0;"></div>
</div>
</div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<div class="vs-table-wrap">
<div class="vs-table-inner">
<div id="s8-header">
<table>
<thead>
<tr>
<th class="col-id">ID</th>
<th>Name</th>
<th>Email</th>
<th class="col-status">Status</th>
</tr>
</thead>
</table>
</div>
<div id="list8" style="height:360px;overflow-y:auto"></div>
</div>
</div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>const vs = new HCGVirtualScroll(rows, {
container: '#list8',
itemHeight: 44,
keyField: 'id',
renderItem(item) {
return `<table><tr>
<td class="col-id">${item.id}</td>
<td>${item.name}</td>
<td>${item.email}</td>
<td class="col-status">${item.status}</td>
</tr></table>`;
},
onResize() { syncHeader(); },
});
function syncHeader() {
const list = document.getElementById('list8');
const header = document.getElementById('s8-header');
header.style.paddingRight = (list.offsetWidth - list.clientWidth) + 'px';
}
syncHeader();</code></pre>
</details>
</section>
<!-- =
DEMO 9 - Live Search Results (filter + updateData)
= -->
<section class="demo-section" id="demo-search">
<h2>9. Live search filtering <span class="tag">updateData()</span></h2>
<p class="demo-desc">Filter a 50,000-row list as you type. On each keystroke a new filtered array is passed to <code>updateData()</code>. When nothing matches, the built-in empty state is shown.</p>
<div class="panel">
<span class="panel-label">Try it</span>
<p class="demo-meta">50 000 rows · type to filter · pass a new array to updateData on every keystroke</p>
<div class="stats-bar">
<span>Query: <b id="s9-query">(none)</b></span>
<span>Matches: <b id="s9-matches">50000</b></span>
<span>Visible: <b id="s9-start">-</b> - <b id="s9-end">-</b></span>
<span>DOM nodes: <b id="s9-dom">-</b></span>
</div>
<div class="btn-row">
<input type="text" id="s9-input" placeholder="Search by name or email…" style="flex:1">
<button class="demo-btn demo-btn--danger" type="button" onclick="s9Clear()">clear</button>
</div>
<div id="list9" class="scroll-list"></div>
</div>
<details class="demo-code">
<summary>View usage code</summary>
<p class="demo-code-label">HTML</p>
<pre><code><link rel="stylesheet" href="hcg-virtual-scroll.css">
<input type="text" id="search-input" placeholder="Search by name or email…">
<div id="list9" style="height:360px;overflow-y:auto"></div>
<script src="hcg-virtual-scroll.js"></script></code></pre>
<p class="demo-code-label">JavaScript</p>
<pre><code>searchInput.addEventListener('input', function (e) {
const q = e.target.value.toLowerCase();
const filtered = allItems.filter(item =>
item.name.toLowerCase().includes(q) ||
item.email.toLowerCase().includes(q)
);
vs.updateData(filtered);
});</code></pre>
</details>
</section>
</div>
<script src="hcg-virtual-scroll.js"></script>
<script>
// - Shared helpers -
const COLORS = ['#6366f1','#10b981','#f59e0b','#ef4444','#3b82f6','#8b5cf6','#ec4899'];
const BADGES = [['Active','badge-green'],['Pending','badge-blue'],['Hot','badge-orange'],['VIP','badge-purple']];
const NAMES = ['Alice','Bob','Carol','David','Eve','Frank','Grace','Heidi','Ivan','Judy'];
const SUBS = ['Engineer','Designer','Manager','Analyst','Director','Consultant','Architect'];
const BOT_MSGS = [
'Sure, sounds good!', 'Let me check that for you.', 'Got it, thanks!',
'Can you clarify what you mean?', 'On it!', 'That looks correct to me.',
'Interesting - tell me more.', 'Done!', 'I agree.', 'Not sure about that one.',
];
function rand(n) { return Math.floor(Math.random() * n); }
function pick(arr) { return arr[rand(arr.length)]; }
function initials(name) { return name.split(' ').map(p => p[0]).join('').toUpperCase().slice(0, 2); }
function domCount(id) { return document.querySelector('#' + id + ' .hcg-vs-content')?.children.length ?? 0; }
function timeStr() { return new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); }
// - Dataset generators -
function makeBasicItems(n, startId = 0) {
return Array.from({ length: n }, (_, i) => ({
id: startId + i,
name: `${pick(NAMES)} ${pick(NAMES)}`,
badge: BADGES[rand(BADGES.length)],
}));
}
function makeDynamicItems(n) {
const heights = [44, 60, 80, 96];
return Array.from({ length: n }, (_, i) => {
const h = pick(heights);
const lines = Math.round((h - 28) / 16);
return {
id: i,
title: `Item #${i} - ${pick(NAMES)} ${pick(NAMES)}`,
body: lines > 0 ? Array.from({ length: lines }, () =>
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.').join(' ') : '',
height: h,
};
});
}
function makeCardItems(n) {
return Array.from({ length: n }, (_, i) => ({
id: i,
name: `${pick(NAMES)} ${pick(NAMES)}`,
sub: `${pick(SUBS)} · ID #${1000 + i}`,
color: COLORS[rand(COLORS.length)],
score: (Math.random() * 10).toFixed(1),
badge: BADGES[rand(BADGES.length)],
}));
}
function makeConfigItems(n) {
return Array.from({ length: n }, (_, i) => ({
id: i, label: `Row #${i} - ${pick(NAMES)} ${pick(NAMES)}`,
}));
}
function makeChatMessage(id, author, text) {
const lines = Math.ceil(text.length / 45);
return { id, author, text, time: timeStr(), height: 54 + lines * 20 };
}
function makeHistoryMessages(n, beforeId) {
const texts = [
'Hey, how are you?', 'Did you see the latest update?', 'Can we reschedule?',
'Sounds great!', 'What do you think about this approach?', 'Almost done with it.',
'Let me know when you are free.', 'Check out this link!', 'Thanks for the help.',
'I will follow up tomorrow.', 'Agreed.', 'That works for me.',
];
return Array.from({ length: n }, (_, i) => {
const text = pick(texts);
return makeChatMessage(beforeId - n + i, pick(['me', 'bot']), text);
});
}
// - DEMO 1 - Fixed height -
const items1 = makeBasicItems(10_000);
const vs1 = new HCGVirtualScroll(items1, {
container: '#list1',
itemHeight: 50,
bufferSize: 3,
adaptiveOverscan: true,
ariaLabel: 'Fixed height list',
renderItem(item, i) {
const [label, cls] = item.badge;
return `<div class="hcg-vs-item-basic">
<span class="idx">#${i}</span>
<span class="name">${item.name}</span>
<span class="badge ${cls}">${label}</span>
</div>`;
},
onScroll(scrollTop, range) {
document.getElementById('s1-scroll').textContent = Math.round(scrollTop);
document.getElementById('s1-start').textContent = range.start;
document.getElementById('s1-end').textContent = range.end;
document.getElementById('s1-dom').textContent = domCount('list1');
},
});
document.getElementById('s1-dom').textContent = domCount('list1');
const r1 = vs1.getVisibleRange();
document.getElementById('s1-start').textContent = r1.start;
document.getElementById('s1-end').textContent = r1.end;
function s1ScrollTo() {
const idx = parseInt(document.getElementById('s1-idx').value, 10);
if (!isNaN(idx)) vs1.scrollTo(idx);
}
function s1Top() { vs1.scrollToTop(); }
function s1Bottom() { vs1.scrollToBottom(true); }
function s1Range() {
const r = vs1.getVisibleRange();
alert(`getVisibleRange() → start: ${r.start}, end: ${r.end}`);
}
// - DEMO 2 - Dynamic height -
let items2 = makeDynamicItems(5_000);
const vs2 = new HCGVirtualScroll(items2, {