-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathBackToTop.svelte
More file actions
47 lines (39 loc) · 853 Bytes
/
BackToTop.svelte
File metadata and controls
47 lines (39 loc) · 853 Bytes
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
<script>
export let showOnPx = 150;
let hidden = true;
function goTop() {
document.body.scrollIntoView();
}
function scrollContainer() {
return document.documentElement || document.body;
}
function handleOnScroll() {
if (!scrollContainer()) {
return;
}
if (scrollContainer().scrollTop > showOnPx) {
hidden = false;
} else {
hidden = true;
}
}
</script>
<style>
.back-to-top {
opacity: 1;
transition: opacity 0.5s, visibility 0.5s;
position: fixed;
z-index: 99;
right: 20px;
user-select: none;
bottom: 20px;
color: white;
background-color: black;
}
.back-to-top.hidden {
opacity: 0;
visibility: hidden;
}
</style>
<svelte:window on:scroll={handleOnScroll} />
<div class="back-to-top" on:click={goTop} class:hidden>Back to top</div>