Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Scroll to hash and add spacing pure javaScript

I'm using this pure JavaScript function to scroll to but want to add spacing to top, similar to what jquery - [...].offset().top - 160; - does. Any ideas? Much appreciated:

function scrollTo(hash) {
    location.hash = "#" + hash;
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Instead of reloading the whole page (Assign to location.hash does that), you can use the built-in scrollTo function...
Ho! by the way, you were overwriting it. ;)

function scrollMeTo(hash) {
  let hashEl = document.querySelector("#" + hash);
  if (hashEl) {
    let hashPos = parseInt(hashEl.getBoundingClientRect().top) - 160;
    window.scrollTo(0, hashPos);
  }
}

scrollMeTo("two");
.spacer{
  height: 1000px;
}
<div class="spacer"></div>
<a id="one">ONE</a>
<div class="spacer"></div>
<a id="two">TWO</a>
<div class="spacer"></div>
<a id="three">THREE</a>
<div class="spacer"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!