Skip to content

Commit 6124f63

Browse files
committed
added infinite scroll demo
1 parent 36c8d85 commit 6124f63

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

test/infinite.html

+83
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+
htm, 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)