Skip to content

滚动到指定位置并进行偏移

Published: at 11:12 AMSuggest Changes

滚动到某个位置并且偏移

function scrollToTargetAdjusted(){
    var element = document.getElementById('targetElement');
    var headerOffset = 45;
    var elementPosition = element.getBoundingClientRect().top;
    var offsetPosition = elementPosition + window.pageYOffset - headerOffset;
  
    window.scrollTo({
         top: offsetPosition,
         behavior: "smooth"
    });
}

Previous Post
iOS WebView 忽略 SSL 证书错误
Next Post
Android 播放按钮适配问题