-
Notifications
You must be signed in to change notification settings - Fork 25
/
Scroller.svelte
222 lines (180 loc) · 4.88 KB
/
Scroller.svelte
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
<script context="module">
const handlers = [];
let manager;
if (typeof window !== 'undefined') {
const run_all = () => handlers.forEach(fn => fn());
window.addEventListener('scroll', run_all);
window.addEventListener('resize', run_all);
}
if (typeof IntersectionObserver !== 'undefined') {
const map = new Map();
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
const update = map.get(entry.target);
const index = handlers.indexOf(update);
if (entry.isIntersecting) {
if (index === -1) handlers.push(update);
} else {
update();
if (index !== -1) handlers.splice(index, 1);
}
});
}, {
rootMargin: '400px 0px' // TODO why 400?
});
manager = {
add: ({ outer, update }) => {
const { top, bottom } = outer.getBoundingClientRect();
if (top < window.innerHeight && bottom > 0) handlers.push(update);
map.set(outer, update);
observer.observe(outer);
},
remove: ({ outer, update }) => {
const index = handlers.indexOf(update);
if (index !== -1) handlers.splice(index, 1);
map.delete(outer);
observer.unobserve(outer);
}
};
} else {
manager = {
add: ({ update }) => {
handlers.push(update);
},
remove: ({ update }) => {
const index = handlers.indexOf(update);
if (index !== -1) handlers.splice(index, 1);
}
};
}
</script>
<script>
import { onMount } from 'svelte';
// config
export let top = 0;
export let bottom = 1;
export let threshold = 0.5;
export let query = 'section';
export let parallax = false;
// bindings
export let index = 0;
export let count = 0;
export let offset = 0;
export let progress = 0;
export let visible = false;
let outer;
let foreground;
let background;
let left;
let sections;
let wh = 0;
let fixed;
let offset_top = 0;
let width = 1;
let height;
let inverted;
$: top_px = Math.round(top * wh);
$: bottom_px = Math.round(bottom * wh);
$: threshold_px = Math.round(threshold * wh);
$: (top, bottom, threshold, parallax, update());
$: style = `
position: ${fixed ? 'fixed' : 'absolute'};
top: 0;
transform: translate(0, ${offset_top}px);
z-index: ${inverted ? 3 : 1};
`;
$: widthStyle = fixed ? `width:${width}px;` : '';
onMount(() => {
sections = foreground.querySelectorAll(query);
count = sections.length;
update();
const scroller = { outer, update };
manager.add(scroller);
return () => manager.remove(scroller);
});
function update() {
if (!foreground) return;
// re-measure outer container
const bcr = outer.getBoundingClientRect();
left = bcr.left;
width = bcr.right - left;
// determine fix state
const fg = foreground.getBoundingClientRect();
const bg = background.getBoundingClientRect();
visible = fg.top < wh && fg.bottom > 0;
const foreground_height = fg.bottom - fg.top;
const background_height = bg.bottom - bg.top;
const available_space = bottom_px - top_px;
progress = (top_px - fg.top) / (foreground_height - available_space);
if (progress <= 0) {
offset_top = 0;
fixed = false;
} else if (progress >= 1) {
offset_top = parallax
? (foreground_height - background_height)
: (foreground_height - available_space);
fixed = false;
} else {
offset_top = parallax ?
Math.round(top_px - progress * (background_height - available_space)) :
top_px;
fixed = true;
}
for (let i = 0; i < sections.length; i++) {
const section = sections[i];
const { top } = section.getBoundingClientRect();
const next = sections[i + 1];
const bottom = next ? next.getBoundingClientRect().top : fg.bottom;
offset = (threshold_px - top) / (bottom - top);
if (bottom >= threshold_px) {
index = i;
break;
}
}
}
</script>
<svelte:window bind:innerHeight={wh}/>
<svelte-scroller-outer bind:this={outer}>
<svelte-scroller-background-container class='background-container' style="{style}{widthStyle}">
<svelte-scroller-background bind:this={background}>
<slot name="background"></slot>
</svelte-scroller-background>
</svelte-scroller-background-container>
<svelte-scroller-foreground bind:this={foreground}>
<slot name="foreground"></slot>
</svelte-scroller-foreground>
</svelte-scroller-outer>
<style>
svelte-scroller-outer {
display: block;
position: relative;
}
svelte-scroller-background {
display: block;
position: relative;
width: 100%;
}
svelte-scroller-foreground {
display: block;
position: relative;
z-index: 2;
}
svelte-scroller-foreground::after {
content: ' ';
display: block;
clear: both;
}
svelte-scroller-background-container {
display: block;
position: absolute;
width: 100%;
max-width: 100%;
pointer-events: none;
/* height: 100%; */
/* in theory this helps prevent jumping */
will-change: transform;
/* -webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0); */
}
</style>