-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scrollmation.svelte
215 lines (190 loc) · 5.73 KB
/
Scrollmation.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
<script context="module">
export const toHomeRatio = ({ homeScrollPos, scrollPosPx, startScrollPosPx }) =>
(homeScrollPos - scrollPosPx) / (homeScrollPos - startScrollPosPx);
export const toStartRatio = ({ homeScrollPos, scrollPosPx, startScrollPosPx }) =>
(scrollPosPx - startScrollPosPx) / (homeScrollPos - startScrollPosPx);
export const toEndRatio = ({ scrollPosPx, endScrollPosPx, homeScrollPos }) =>
(scrollPosPx - endScrollPosPx) / (homeScrollPos - endScrollPosPx);
export const toRangeRatio = ({ scrollPosPx, endScrollPosPx, startScrollPosPx }) =>
(scrollPosPx - endScrollPosPx) / (startScrollPosPx - endScrollPosPx);
export const fullRangePx = ({ endScrollPosPx, startScrollPosPx }) =>
endScrollPosPx - startScrollPosPx;
export const toHomePx = ({ homeScrollPos, scrollPosPx }) =>
homeScrollPos - scrollPosPx;
export const toEndPx = ({ endScrollPosPx, scrollPosPx }) =>
endScrollPosPx - scrollPosPx;
export const toStartPx = ({ startScrollPosPx, scrollPosPx }) =>
startScrollPosPx - scrollPosPx;
</script>
<script>
import { afterUpdate, createEventDispatcher, onMount } from "svelte";
import { tweened } from "svelte/motion";
import { cubicOut, linear } from "svelte/easing";
import { get_current_component } from "svelte/internal";
const component = get_current_component();
const svelteDispatch = createEventDispatcher();
const dispatch = (name, detail) => {
svelteDispatch(name, detail);
component.dispatchEvent &&
component.dispatchEvent(
new CustomEvent(name, {
detail,
bubbles: true,
cancelable: true,
composed: true // makes the event jump shadow DOM boundary
})
);
};
let contentHeight;
let containerHeight;
let container;
let scrollPosPx = 0;
let animatingScroll = false;
let endScrollPosPx;
let startScrollPosPx = 0;
let prevScrollPosPx;
let targetScrollPx;
let scrollDir;
let targetPos = "home";
let loading = true;
let scrolldata = {};
export let startpos = 0; // px past the end
export let homepos = 0; //px from the top
export let endpos = 0; // px above the top - or negative value for before the end
export let duration = 900;
export let easing = cubicOut;
export let isprevnav = false;
export let scrolltoposition = null;
export let jumptoposition = null;
export let pgId = 0;
async function onScroll(e) {
prevScrollPosPx = scrollPosPx;
scrollPosPx = e.target.scrollTop;
if (!animatingScroll) {
progress.set(scrollPosPx, { duration: 0 });
}
let action = null;
if (prevScrollPosPx !== scrollPosPx) {
dispatch("scroll", scrolldata, pgId);
if (scrollPosPx === endScrollPosPx) {
action = "next";
}
if (scrollPosPx === startScrollPosPx) {
action = "prev";
}
if (scrollPosPx === homeScrollPos) {
action = "home";
}
dispatch(action, scrolldata);
}
}
function onWheel(e) {
if (animatingScroll) {
e.preventDefault();
}
}
export let progress = tweened(0, {
duration,
easing
});
export async function jumpToPos(destPos) {
return await scrollToPos(destPos, false);
}
export async function scrollToPos(destPos = "home", anim = true) {
if (!destPos) {
return;
}
targetPos = destPos;
switch (destPos) {
case "offscreen":
targetScrollPx = endScrollPosPx + 100;
break;
case "start":
targetScrollPx = startScrollPosPx;
break;
case "end":
targetScrollPx = endScrollPosPx;
break;
case "beforeStart":
targetScrollPx = startScrollPosPx + 1;
break;
case "beforeEnd":
targetScrollPx = endScrollPosPx - 1;
break;
default:
targetScrollPx = homeScrollPos;
break;
}
if (anim) {
animatingScroll = true;
await progress.set(targetScrollPx);
animatingScroll = false;
} else {
await progress.set(targetScrollPx, { duration: 0 });
}
}
// this needs some explaining. what problem am I solving here? initialisation of the frame at some point
// providing a natural flow when going back
async function initPos(p) {
setTimeout(async () => {
if (isprevnav) {
await scrollToPos("beforeEnd", false);
} else {
await scrollToPos("beforeStart", false);
}
loading = false;
await scrollToPos("home");
}, 200);
}
$: if (animatingScroll) container.scrollTop = $progress;
$: endScrollPosPx = containerHeight + contentHeight + endpos + startpos;
$: homeScrollPos = containerHeight - homepos + startpos;
$: scrollDir = scrollPosPx - prevScrollPosPx;
$: scrollToPos(scrolltoposition);
$: jumpToPos(jumptoposition);
$: initPos(pgId);
$: {
scrolldata = {
contentHeight,
containerHeight,
scrollPosPx,
endScrollPosPx,
startScrollPosPx,
homeScrollPos,
scrollDir,
animatingScroll,
};
}
</script>
<style>
.scrollmation-container {
width: 100%;
height: 100%;
overflow: auto;
scrollbar-width: none;
overscroll-behavior: none;
}
.scrollmation-container::-webkit-scrollbar {
display: none;
}
.scrollmation-scroll-spacer {
height: 1px;
}
</style>
<div
class="scrollmation-container"
bind:this={container}
bind:clientHeight={containerHeight}
on:scroll={onScroll}
on:wheel={onWheel}
style="opacity: {loading ? 0 : 1}">
{#if !loading}
<div
class="scrollmation-fg"
bind:clientHeight={contentHeight}
style="margin-top: {containerHeight + startpos}px; margin-bottom: {containerHeight + endpos}px">
<slot />
<div class="scrollmation-scroll-spacer"> </div>
</div>
{/if}
</div>