forked from ayamflow/virtual-scroll
/
example.html
76 lines (66 loc) · 1.5 KB
/
example.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
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Virtual scroll example</title>
<style>
body {
overflow: hidden;
margin: 0;
}
section {
height: 90vh;
background-color: black;
}
section.s-2 {
background-color: aqua;
}
section.s-3 {
background-color: burlywood;
}
section.s-4 {
height: 100vh;
background-color: darkcyan;
}
</style>
</head>
<body>
<div id="app">
<section></section>
<section class="s-2"></section>
<section class="s-3"></section>
<section class="s-4"></section>
</div>
<script src="./dist/index.js"></script>
<script>
const instance = new VirtualScroll({mouseMultiplier: .5})
let targetY = 0
let currentY = 0
let app = document.querySelector('#app')
let appHeight = app.getBoundingClientRect().height
const ease = 0.1
instance.on((e) => {
targetY += e.deltaY
targetY = Math.max(-(appHeight - window.innerHeight), targetY)
targetY = Math.min(0, targetY)
})
function run() {
requestAnimationFrame(run)
currentY += (targetY - currentY) * ease
currentY = parseFloat(currentY.toFixed(4))
const t = `translateY(${currentY}px) translateZ(0)`
const s = app.style
s['transform'] = t
s['webkitTransform'] = t
s['mozTransform'] = t
s['msTransform'] = t
}
function onResize() {
appHeight = app.getBoundingClientRect().height
targetY = 0
}
window.addEventListener('resize', onResize)
run()
</script>
</body>
</html>