Answer the question
In order to leave comments, you need to log in
How to scroll the page a few pixels up?
When you click on the anchor link, the browser does not scroll to the very top of the page and there is a small segment from the other section. How to make sure that this segment does not exist?
I tried to do this using window.scrollBy, but I couldn’t fit it into the scrolling code itself
const anchors = document.querySelectorAll('a[href*="#"]')
for (let anchor of anchors) {
anchor.addEventListener("click", function(event) {
event.preventDefault();
const blockID = anchor.getAttribute('href')
document.querySelector('' + blockID).scrollIntoView({
behavior: "smooth",
block: "start"
})
})
}
Answer the question
In order to leave comments, you need to log in
I think you have margin-top , you need to replace it with padding-top
Try it, it might work
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question