File tree 1 file changed +18
-9
lines changed 1 file changed +18
-9
lines changed Original file line number Diff line number Diff line change 18
18
<script >
19
19
import throttle from ' throttle-debounce/throttle' ;
20
20
21
+ const cubic = value => Math .pow (value, 3 );
22
+ const easeInOutCubic = value => value < 0.5
23
+ ? cubic (value * 2 ) / 2
24
+ : 1 - cubic ((1 - value) * 2 ) / 2 ;
25
+
21
26
export default {
22
27
name: ' ElBacktop' ,
23
28
@@ -81,16 +86,20 @@ export default {
81
86
this .$emit (' click' , e);
82
87
},
83
88
scrollToTop () {
84
- let el = this .el ;
85
- let step = 0 ;
86
- let interval = setInterval (() => {
87
- if (el .scrollTop <= 0 ) {
88
- clearInterval (interval);
89
- return ;
89
+ const el = this .el ;
90
+ const beginTime = Date .now ();
91
+ const beginValue = el .scrollTop ;
92
+ const rAF = window .requestAnimationFrame || (func => setTimeout (func, 16 ));
93
+ const frameFunc = () => {
94
+ const progress = (Date .now () - beginTime) / 500 ;
95
+ if (progress < 1 ) {
96
+ el .scrollTop = beginValue * (1 - easeInOutCubic (progress));
97
+ rAF (frameFunc);
98
+ } else {
99
+ el .scrollTop = 0 ;
90
100
}
91
- step += 10 ;
92
- el .scrollTop -= step;
93
- }, 20 );
101
+ };
102
+ rAF (frameFunc);
94
103
}
95
104
},
96
105
You can’t perform that action at this time.
0 commit comments