Commit b6b9aaec authored by wurong's avatar wurong Committed by GitHub

Merge pull request #17057 from cnlon/optimize/scrolling-of-backtop

BackTop: use requestAnimationFrame and cubic bezier for scrolling
parents c9f4e8fd d961d9d5
...@@ -18,6 +18,11 @@ ...@@ -18,6 +18,11 @@
<script> <script>
import throttle from 'throttle-debounce/throttle'; import throttle from 'throttle-debounce/throttle';
const cubic = value => Math.pow(value, 3);
const easeInOutCubic = value => value < 0.5
? cubic(value * 2) / 2
: 1 - cubic((1 - value) * 2) / 2;
export default { export default {
name: 'ElBacktop', name: 'ElBacktop',
...@@ -81,16 +86,20 @@ export default { ...@@ -81,16 +86,20 @@ export default {
this.$emit('click', e); this.$emit('click', e);
}, },
scrollToTop() { scrollToTop() {
let el = this.el; const el = this.el;
let step = 0; const beginTime = Date.now();
let interval = setInterval(() => { const beginValue = el.scrollTop;
if (el.scrollTop <= 0) { const rAF = window.requestAnimationFrame || (func => setTimeout(func, 16));
clearInterval(interval); const frameFunc = () => {
return; const progress = (Date.now() - beginTime) / 500;
if (progress < 1) {
el.scrollTop = beginValue * (1 - easeInOutCubic(progress));
rAF(frameFunc);
} else {
el.scrollTop = 0;
} }
step += 10; };
el.scrollTop -= step; rAF(frameFunc);
}, 20);
} }
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment