📌  相关文章
📜  javascript 滚动到具有偏移量的元素 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:31.282000             🧑  作者: Mango

代码示例1
const element = document.getElementById('targetElement');
const offset = 45;
const bodyRect = document.body.getBoundingClientRect().top;
const elementRect = element.getBoundingClientRect().top;
const elementPosition = elementRect - bodyRect;
const offsetPosition = elementPosition - offset;

window.scrollTo({
  top: offsetPosition,
  behavior: 'smooth'
});