缓缓滚动到顶部实现方式
scrollToTop() {
const cubic = value => Math.pow(value, 3);
const easeInOutCubic = value => (value < 0.5 ? cubic(value * 2) / 2 : 1 - cubic((1 - value) * 2) / 2);
const beginTime = Date.now();
const beginValue = this.scrollToTopNum;
// const rAF = wx.requestAnimationFrame || (func => setTimeout(func, 16));
const rAF = func => setTimeout(func, 16);
const frameFunc = () => {
const progress = (Date.now() - beginTime) / 500;
if (progress < 1) {
this.scrollToTopNum = beginValue * (1 - easeInOutCubic(progress));
rAF(frameFunc);
} else {
this.scrollToTopNum = 0;
}
};
rAF(frameFunc);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
上次更新: 2022/06/05, 20:31:36