forked from domvm/domvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfinite-scroll.html
71 lines (58 loc) · 1.57 KB
/
infinite-scroll.html
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
<!doctype html>
<html>
<head>
</head>
<body>
<script src="../dist/nano/domvm.nano.js"></script>
<script>
var el = domvm.defineElement,
tx = domvm.defineText,
cm = domvm.defineComment,
vw = domvm.defineView,
iv = domvm.injectView,
ie = domvm.injectElement;
const itemsToLoad = 20;
function ItemListView(vm, model) {
const itemHeight = 30;
const windowHeight = 300;
const loadWhenNumFromBottom = 5;
function calcLoadLine(scrollHeight) {
return scrollHeight - (itemHeight * loadWhenNumFromBottom);
}
var loadLine = calcLoadLine(itemsToLoad * itemHeight);
var onscroll = function(e) {
var winBottom = e.target.scrollTop + windowHeight;
if (winBottom >= loadLine) {
model.loadMoar();
vm.redraw();
loadLine = calcLoadLine(e.target.scrollHeight);
}
};
// this is render() and is called on every redraw() to regen the template
return function() {
var props = {
style: {height: windowHeight, overflowY: "scroll"},
onscroll: onscroll,
};
var kids = model.items.map(function(item) {
return el("div", {style: {color: item.color, height: itemHeight}}, item.color);
});
return el("#win", props, kids);
}
}
function randColor() {
return '#'+Math.floor(Math.random()*16777215).toString(16);
}
var model = {
loadMoar: function() {
for (var i = itemsToLoad; i > 0; i--)
model.items.push({color: randColor()});
},
items: [],
};
// load initial items
model.loadMoar();
domvm.createView(ItemListView, model).mount(document.body);
</script>
</body>
</html>