I have a site with a header set to position: fixed
. On one of my pages, I use scrollIntoView(true)
on an element. My problem is that when scr
In case if someone has fixed navbar which hides Header\Title after scrolling, here is the solution (based on @coco puffs
's answer and this one):
let anchorLinks = document.querySelectorAll('a[href^="#"]')
for (let item of anchorLinks) {
item.addEventListener('click', (e) => {
let hashVal = item.getAttribute('href')
let topOfElement = document.querySelector(hashVal).offsetTop - 70
window.scroll({ top: topOfElement, behavior: "smooth" })
history.pushState(null, null, hashVal)
e.preventDefault()
})
}
In the code 70px
are used.