Javascript页面滚动:单击按钮,然后将页面平滑滚动到特定部分

时间:2020-09-20 06:22:42

标签: javascript scroll

请帮助我调试enter image description here

这是我的代码

  const target = document.querySelector(eventTarget);
  const targetPosition = target.getBoundingClientRect();
  const startPosition = window.pageYoffset;
  const distance = targetPosition - startPosition;
  const startTime = null;
  function animation(currentTime) {
    if(startTime === null )
    startTime = currentTime;
    const timeElapsed = currentTime - startTime
    const run = ease(timeElapsed, startPosition, distance, duration);
  }

  function ease(t,b,c,d) {
    t /= d / 2;
    if (t < 1) return c / 2 * t * t + b;
    t--;
    return -c / 2 * (t * (t-2) - 1) + b;
  }

  requestAnimationFrame(animation);
}



const clickScroll = () => {
  const button = document.getElementById(".button");
  button.addEventListener("click", () => {
  smoothScroll(".second", 1000);
});
}

export { smoothScroll, clickScroll };

因此,基于控制台,target = document.querySelector(eventTarget);无法工作,因此它为null。 请帮助调试该漏洞并阐明该错误

0 个答案:

没有答案