Skip to content

Commit 3ff7b77

Browse files
committed
added infinite scroll demo
1 parent 36c8d85 commit 3ff7b77

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

hyperhtml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ var hyperHTML = (function (globalDocument, majinbuu) {'use strict';
11931193
// changes needed to show the a new list
11941194
// where there was another one.
11951195
// There is a limit, in terms of performance,
1196-
// on how bug can the optimal computation be,
1196+
// on how big can the optimal computation be,
11971197
// so if you change this value be sure your
11981198
// target hardware is good enough.
11991199
hyper.MAX_LIST_SIZE = 1000;

test/infinite.html

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Infinite Scroll</title>
7+
<script defer src="../min.js"></script>
8+
<script>
9+
addEventListener('DOMContentLoaded', () => {
10+
var view = document.querySelector('.infinite');
11+
var scrollTop = 0;
12+
view.addEventListener('mousewheel', (e) => {
13+
scrollTop += e.deltaY;
14+
if (scrollTop < 0) scrollTop = 0;
15+
update();
16+
}, {passive: true});
17+
var render = hyperHTML.bind(view);
18+
var list = [];
19+
var sampleHeight = 60;
20+
var colors = [];
21+
update();
22+
function color(i) {
23+
return colors[i] || (colors[i] = `rgb(${[
24+
Math.round(Math.random() * 155 + 150),
25+
Math.round(Math.random() * 155 + 150),
26+
Math.round(Math.random() * 155 + 150)
27+
].join(',')})`);
28+
}
29+
function update() {
30+
var i = Math.max(0, Math.floor(scrollTop / sampleHeight));
31+
var count = 0;
32+
var newList = [];
33+
var clientHeight = view.clientHeight;
34+
while ((count * sampleHeight) < (clientHeight + sampleHeight)) {
35+
newList[count] = list[i] || (list[i] = hyperHTML
36+
`<p style="${'background:' + color(i)}">Value ${Math.random()}</p>`);
37+
count++;
38+
i++;
39+
}
40+
render`${newList}`;
41+
view.scrollTop = scrollTop % sampleHeight;
42+
}
43+
}, {once: true});
44+
</script>
45+
<style>
46+
html, body {
47+
margin: 0;
48+
padding: 0;
49+
box-sizing: border-box;
50+
}
51+
div {
52+
margin: auto;
53+
height: 100vh;
54+
max-width: 320px;
55+
background: #eee;
56+
}
57+
.infinite {
58+
padding: 0;
59+
overflow: hidden;
60+
box-sizing: border-box;
61+
}
62+
.infinite > p {
63+
text-align: center;
64+
line-height: 48px;
65+
padding: 0;
66+
margin: 0;
67+
border: 8px solid silver;
68+
vertical-align: middle;
69+
border-bottom: none;
70+
height: 60px;
71+
font-family: sans-serif;
72+
}
73+
.infinite > *,
74+
.infinite > *:before,
75+
.infinite > *:after {
76+
box-sizing: inherit;
77+
}
78+
</style>
79+
</head>
80+
<body>
81+
<div class="infinite"></div>
82+
</body>
83+
</html>

0 commit comments

Comments
 (0)