Skip to content

Commit bda69a3

Browse files
committed
feat: ✨ add back to top
1 parent 3427821 commit bda69a3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/routes/+page.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import { Head } from 'svead'
2525
import { onMount } from 'svelte'
2626
import { writable } from 'svelte/store'
27+
import BackToTop from './back-to-top.svelte'
2728

2829
// Head variables
2930
let title = 'This is SvelteKit embed'
@@ -69,6 +70,8 @@
6970

7071
<!-- cSpell:ignore yyoood,Zokm,Zrzbx -->
7172

73+
<BackToTop />
74+
7275
# SvelteKit Embed
7376

7477
This is a collection of embed components I use on a regular basis

src/routes/back-to-top.svelte

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts">
2+
import { fly } from 'svelte/transition'
3+
4+
let showScrollButton = false
5+
6+
function scrollToTop() {
7+
window.scrollTo({
8+
top: 0,
9+
behavior: 'smooth',
10+
})
11+
}
12+
13+
function handleScroll() {
14+
showScrollButton = window.pageYOffset > 0
15+
}
16+
</script>
17+
18+
<svelte:window on:scroll={handleScroll} />
19+
20+
{#if showScrollButton}
21+
<button
22+
on:click={scrollToTop}
23+
transition:fly={{ y: 200, duration: 300 }}
24+
class="fixed bottom-8 right-8 btn btn-primary normal-case"
25+
>
26+
Back to top
27+
</button>
28+
{/if}

0 commit comments

Comments
 (0)