提交 | 用户 | age
|
36b711
|
1 |
<template> |
L |
2 |
<transition :name="transitionName"> |
|
3 |
<div v-show="visible" :style="customStyle" class="back-to-ceiling" @click="backToTop"> |
|
4 |
<svg width="16" height="16" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg" class="Icon Icon--backToTopArrow" aria-hidden="true" style="height:16px;width:16px"><path d="M12.036 15.59a1 1 0 0 1-.997.995H5.032a.996.996 0 0 1-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z" /></svg> |
|
5 |
</div> |
|
6 |
</transition> |
|
7 |
</template> |
|
8 |
|
|
9 |
<script> |
|
10 |
export default { |
|
11 |
name: 'BackToTop', |
|
12 |
props: { |
|
13 |
visibilityHeight: { |
|
14 |
type: Number, |
|
15 |
default: 400 |
|
16 |
}, |
|
17 |
backPosition: { |
|
18 |
type: Number, |
|
19 |
default: 0 |
|
20 |
}, |
|
21 |
customStyle: { |
|
22 |
type: Object, |
|
23 |
default: function() { |
|
24 |
return { |
|
25 |
right: '50px', |
|
26 |
bottom: '50px', |
|
27 |
width: '40px', |
|
28 |
height: '40px', |
|
29 |
'border-radius': '4px', |
|
30 |
'line-height': '45px', |
|
31 |
background: '#e7eaf1' |
|
32 |
} |
|
33 |
} |
|
34 |
}, |
|
35 |
transitionName: { |
|
36 |
type: String, |
|
37 |
default: 'fade' |
|
38 |
} |
|
39 |
}, |
|
40 |
data() { |
|
41 |
return { |
|
42 |
visible: false, |
|
43 |
interval: null, |
|
44 |
isMoving: false |
|
45 |
} |
|
46 |
}, |
|
47 |
mounted() { |
|
48 |
window.addEventListener('scroll', this.handleScroll) |
|
49 |
}, |
|
50 |
beforeDestroy() { |
|
51 |
window.removeEventListener('scroll', this.handleScroll) |
|
52 |
if (this.interval) { |
|
53 |
clearInterval(this.interval) |
|
54 |
} |
|
55 |
}, |
|
56 |
methods: { |
|
57 |
handleScroll() { |
|
58 |
this.visible = window.pageYOffset > this.visibilityHeight |
|
59 |
}, |
|
60 |
backToTop() { |
|
61 |
if (this.isMoving) return |
|
62 |
const start = window.pageYOffset |
|
63 |
let i = 0 |
|
64 |
this.isMoving = true |
|
65 |
this.interval = setInterval(() => { |
|
66 |
const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500)) |
|
67 |
if (next <= this.backPosition) { |
|
68 |
window.scrollTo(0, this.backPosition) |
|
69 |
clearInterval(this.interval) |
|
70 |
this.isMoving = false |
|
71 |
} else { |
|
72 |
window.scrollTo(0, next) |
|
73 |
} |
|
74 |
i++ |
|
75 |
}, 16.7) |
|
76 |
}, |
|
77 |
easeInOutQuad(t, b, c, d) { |
|
78 |
if ((t /= d / 2) < 1) return c / 2 * t * t + b |
|
79 |
return -c / 2 * (--t * (t - 2) - 1) + b |
|
80 |
} |
|
81 |
} |
|
82 |
} |
|
83 |
</script> |
|
84 |
|
|
85 |
<style scoped> |
|
86 |
.back-to-ceiling { |
|
87 |
position: fixed; |
|
88 |
display: inline-block; |
|
89 |
text-align: center; |
|
90 |
cursor: pointer; |
|
91 |
} |
|
92 |
|
|
93 |
.back-to-ceiling:hover { |
|
94 |
background: #d5dbe7; |
|
95 |
} |
|
96 |
|
|
97 |
.fade-enter-active, |
|
98 |
.fade-leave-active { |
|
99 |
transition: opacity .5s; |
|
100 |
} |
|
101 |
|
|
102 |
.fade-enter, |
|
103 |
.fade-leave-to { |
|
104 |
opacity: 0 |
|
105 |
} |
|
106 |
|
|
107 |
.back-to-ceiling .Icon { |
|
108 |
fill: #9aaabf; |
|
109 |
background: none; |
|
110 |
} |
|
111 |
</style> |