下面是组件部分:
里面分装了三个不同速度的方法使用哪个直接调用即可
import React, { Component } from "react";
import "./backToTop.less";
export default class BackToTop extends Component {
n: number = 0
start: number = 0
time: number = 1000
count: number = 0
dis: number = 0
iTget: number = 0
timer: NodeJS.Timeout | null = null
a: number = 0
cur: number = 0
backTop() {
if (this.timer) {
clearInterval(this.timer)
}
this.n = 0
this.count = Math.floor(this.time / 30)
this.start = document.documentElement.scrollTop
this.dis = this.iTget - this.start
this.timer = setInterval(this.FnRunOne.bind(this),30)
}
// 匀速进行
FnRunOne(){
this.n++
this.a = this.n / this.count
this.cur = this.start + this.dis * this.a
document.documentElement.scrollTop = this.cur
if (this.n === this.count) {
i