-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathGoTop.vue
51 lines (45 loc) · 1014 Bytes
/
GoTop.vue
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
<script setup lang="ts">
const position = ref(0)
/**
* Checks if the position is higher than a specific number and returns a boolean value.
* @returns {boolean} Higher than the given number.
*/
const isActive = computed((): boolean => {
return position.value > 100
})
/**
* Updates the Vue data when it's called.
*/
const updatePosition = () => {
position.value = window.scrollY
}
/**
* Scrolls window to top.
*/
const goTop = () => {
window.scrollTo(0, 0)
}
onMounted(() => {
window.addEventListener("scroll", updatePosition)
})
onBeforeUnmount(() => {
window.removeEventListener("scroll", updatePosition)
})
</script>
<template>
<Transition name="fade">
<div
v-show="isActive"
class="right-6 bottom-4 z-50 fixed items-center md:flex md:space-x-2"
>
<Button
backdrop
rounded
elevated
icon="heroicons:chevron-up"
@click="goTop"
/>
<ColorSwitcher class="hidden md:flex" />
</div>
</Transition>
</template>