滚动

js滚动到指定位置
const rollTo = (distance = 0, time) => {
// debugger
if (!time) {
scrollTo(0, distance);
return distance;
}
const spacingTime = 20; // 设置循环的间隔时间 值越小消耗性能越高
let index = time / spacingTime; // 计算循环的次数
let now = document.getElementById("details").scrollTop; // 获取当前滚动条位置
let rollDistance = (distance - now) / index; // 计算每次滑动的距离
let rollTimer = setInterval(() => {
if (index > 0) {
index--;
document.getElementById("details").scrollTop += rollDistance;
} else {
clearInterval(rollTimer); // 清除计时器
}
}, spacingTime);
};

  const height = document.getElementById(`content${i + 1}`).offsetTop  - 44
  // rollTo(距离,时间);
  rollTo(height, 200)

你可能感兴趣的:(滚动)