|
10 | 10 | </div> |
11 | 11 | </transition> |
12 | 12 | </template> |
| 13 | + |
13 | 14 | <script> |
14 | | -export default { |
15 | | - name: 'BackToTop', |
16 | | - props: { |
17 | | - visibilityHeight: { |
18 | | - type: Number, |
19 | | - default: 400 |
20 | | - }, |
21 | | - backPosition: { |
22 | | - type: Number, |
23 | | - default: 0 |
| 15 | + export default { |
| 16 | + name: 'BackToTop', |
| 17 | + props: { |
| 18 | + visibilityHeight: { |
| 19 | + type: Number, |
| 20 | + default: 400 |
| 21 | + }, |
| 22 | + backPosition: { |
| 23 | + type: Number, |
| 24 | + default: 0 |
| 25 | + }, |
| 26 | + customStyle: { |
| 27 | + type: Object, |
| 28 | + default: { |
| 29 | + right: '50px', |
| 30 | + bottom: '50px', |
| 31 | + width: '40px', |
| 32 | + height: '40px', |
| 33 | + 'border-radius': '4px', |
| 34 | + 'line-height': '45px', |
| 35 | + background: '#e7eaf1' |
| 36 | + } |
| 37 | + }, |
| 38 | + transitionName: { |
| 39 | + type: String, |
| 40 | + default: 'fade' |
| 41 | + } |
24 | 42 | }, |
25 | | - customStyle: { |
26 | | - type: Object, |
27 | | - default: { |
28 | | - right: '50px', |
29 | | - bottom: '50px', |
30 | | - width: '40px', |
31 | | - height: '40px', |
32 | | - 'border-radius': '4px', |
33 | | - 'line-height': '40px', |
34 | | - background: '#e7eaf1' |
| 43 | + data() { |
| 44 | + return { |
| 45 | + visible: false, |
| 46 | + interval: null |
35 | 47 | } |
36 | 48 | }, |
37 | | - transitionName: { |
38 | | - type: String, |
39 | | - default: 'fade' |
40 | | - } |
41 | | -
|
42 | | - }, |
43 | | - data() { |
44 | | - return { |
45 | | - visible: false, |
46 | | - interval: null |
47 | | - } |
48 | | - }, |
49 | | - mounted() { |
50 | | - window.addEventListener('scroll', this.handleScroll); |
51 | | - }, |
52 | | - beforeDestroy() { |
53 | | - window.removeEventListener('scroll', this.handleScroll); |
54 | | - if (this.interval) { |
55 | | - clearInterval(this.interval); |
56 | | - } |
57 | | - }, |
58 | | - methods: { |
59 | | - handleScroll() { |
60 | | - this.visible = window.pageYOffset > this.visibilityHeight; |
| 49 | + mounted() { |
| 50 | + window.addEventListener('scroll', this.handleScroll); |
61 | 51 | }, |
62 | | - backToTop() { |
63 | | - const start = window.pageYOffset; |
64 | | - let i = 0; |
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 | | - } else { |
71 | | - window.scrollTo(0, next); |
72 | | - } |
73 | | - i++; |
74 | | - }, 16.7) |
| 52 | + beforeDestroy() { |
| 53 | + window.removeEventListener('scroll', this.handleScroll); |
| 54 | + if (this.interval) { |
| 55 | + clearInterval(this.interval); |
| 56 | + } |
75 | 57 | }, |
76 | | - easeInOutQuad(t, b, c, d) { |
77 | | - if ((t /= d / 2) < 1) return c / 2 * t * t + b; |
78 | | - return -c / 2 * (--t * (t - 2) - 1) + b; |
| 58 | + methods: { |
| 59 | + handleScroll() { |
| 60 | + this.visible = window.pageYOffset > this.visibilityHeight; |
| 61 | + }, |
| 62 | + backToTop() { |
| 63 | + const start = window.pageYOffset; |
| 64 | + let i = 0; |
| 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 | + } else { |
| 71 | + window.scrollTo(0, next); |
| 72 | + } |
| 73 | + i++; |
| 74 | + }, 16.7) |
| 75 | + }, |
| 76 | + easeInOutQuad(t, b, c, d) { |
| 77 | + if ((t /= d / 2) < 1) return c / 2 * t * t + b; |
| 78 | + return -c / 2 * (--t * (t - 2) - 1) + b; |
| 79 | + } |
79 | 80 | } |
80 | 81 | } |
81 | | -} |
82 | 82 | </script> |
| 83 | + |
83 | 84 | <style scoped> |
84 | | -.back-to-top { |
85 | | - position: fixed; |
86 | | - display: inline-block; |
87 | | - text-align: center; |
88 | | - cursor: pointer; |
89 | | -} |
| 85 | + .back-to-top { |
| 86 | + position: fixed; |
| 87 | + display: inline-block; |
| 88 | + text-align: center; |
| 89 | + cursor: pointer; |
| 90 | + } |
90 | 91 |
|
91 | | -.back-to-top:hover { |
92 | | - background: #d5dbe7; |
93 | | -} |
| 92 | + .back-to-top:hover { |
| 93 | + background: #d5dbe7; |
| 94 | + } |
94 | 95 |
|
95 | | -.fade-enter-active, |
96 | | -.fade-leave-active { |
97 | | - transition: opacity .5s; |
98 | | -} |
| 96 | + .fade-enter-active, |
| 97 | + .fade-leave-active { |
| 98 | + transition: opacity .5s; |
| 99 | + } |
99 | 100 |
|
100 | | -.fade-enter, |
101 | | -.fade-leave-to { |
102 | | - opacity: 0 |
103 | | -} |
| 101 | + .fade-enter, |
| 102 | + .fade-leave-to { |
| 103 | + opacity: 0 |
| 104 | + } |
104 | 105 |
|
105 | | -.back-to-top .Icon { |
106 | | - fill: #9aaabf; |
107 | | - background: none; |
108 | | -} |
| 106 | + .back-to-top .Icon { |
| 107 | + fill: #9aaabf; |
| 108 | + background: none; |
| 109 | + } |
109 | 110 | </style> |
110 | | - |
111 | | - |
0 commit comments