📜  平滑滚动到具有偏移量的目标 - Javascript 代码示例

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

代码示例1
function scrollToTargetAdjusted(){
    var element = document.getElementById('targetElement');
    var headerOffset = 45;
    var elementPosition = element.getBoundingClientRect().top;
    var offsetPosition = elementPosition - headerOffset;

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