N
N
Norum2021-06-28 20:39:42
JavaScript
Norum, 2021-06-28 20:39:42

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"
      })
    })
  }


The site itself is ilyin1ib.beget.tech

60da095aaae1a428155827.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ravshan Khalimov, 2021-06-28
@Norum

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 question

Ask a Question

731 491 924 answers to any question