-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackToTop.vue
97 lines (82 loc) · 1.53 KB
/
BackToTop.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
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
<template>
<transition name="fade">
<div v-if="show" @click="scrollToTop" class="go-to-top" >
</div>
</transition>
</template>
<script>
import debounce from 'lodash.debounce'
export default {
name: 'BackToTop',
props: {
threshold: {
type: Number,
default: 300
}
},
data () {
return {
scrollTop: null
}
},
computed: {
show () {
return this.scrollTop > this.threshold
}
},
mounted () {
this.scrollTop = this.getScrollTop()
window.addEventListener('scroll', debounce(() => {
this.scrollTop = this.getScrollTop()
}, 100))
},
methods: {
getScrollTop () {
return window.pageYOffset
|| document.documentElement.scrollTop
|| document.body.scrollTop || 0
},
scrollToTop () {
window.scrollTo({ top: 0, behavior: 'smooth' })
this.scrollTop = 0
}
}
}
</script>
<style lang='stylus' scoped>
.go-to-top {
cursor: pointer;
position: fixed;
right: 15px;
bottom: 50px;
width: 60px;
height: 60px;
z-index: 100;
color: $accentColor;
z-index: 1;
background: url(/rocket-icon.png) center center/contain no-repeat;
animation: .4s linear infinite alternate sway;
}
@keyframes sway {
0% {
bottom: 13%
}
100% {
bottom: 10%
}
}
.go-to-top:hover {
color: lighten($accentColor, 30%);
}
@media (max-width: 959px) {
.go-to-top {
display: none;
}
}
.fade-enter-active, .fade-leave-active {
transition: opacity 0.3s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>