Skip to content

Commit d961d9d

Browse files
author
lon
committed
BackTop: use cubic bezier scrolling
1 parent 91297a9 commit d961d9d

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

packages/backtop/src/main.vue

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<script>
1919
import throttle from 'throttle-debounce/throttle';
2020
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+
2126
export default {
2227
name: 'ElBacktop',
2328
@@ -81,16 +86,20 @@ export default {
8186
this.$emit('click', e);
8287
},
8388
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;
90100
}
91-
step += 10;
92-
el.scrollTop -= step;
93-
}, 20);
101+
};
102+
rAF(frameFunc);
94103
}
95104
},
96105

0 commit comments

Comments
 (0)