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

240
Views
Event of reaching the edge of window in correct way (onscroll)

Just in case, its my 1th question.

I have: nav menu with transparent background and trying to change backrgound when reached top edge of window.

window.addEventListener("scroll", navSticky);
function navSticky(){
  let nav = document.getElementById('navbar');
  let bounding = nav.getBoundingClientRect();

  if(bounding.top <= 0 ){
      nav.classList.add("sticky");
  }else{
      nav.classList.remove("sticky");
  }
}
*{
  margin: 0;
  text-align: center;
}

body{
  background: lightgrey;
}

header h1{
  padding: 40px;
}

nav{
  position: sticky;
  top: 0;
  padding: 12px;
  background: linear-gradient(to right, 
  rgba(0,0,0,0),
  rgba(0,255,0, 0.5), 
  rgba(255,0,0, 0.5),
  rgba(0,0,0,0));
  color: black;
}

nav:before{
  content: "now bg transparent";
}

.container{
  min-height: 1000px;
  padding: 20px;
}

nav.sticky{
  background: linear-gradient(to right, 
  rgb(0,255,0), 
  rgb(255,0,0));
  color: white;
}

nav.sticky:before{
  content: "now bg isn't transparent";
}
<body>
  <header>
    <h1>Header, that ask u to scroll page</h1> 
  </header>
  <nav id="navbar">
  </nav>
  <div class ="container">
      When we scroll page, and nav reached top of screen, .sticky add to classList<br>
  </div>
</body>

Its work, but I have several questions:

  1. is it possible to do same without js?
  2. is it possible to optimise this script cuz scroll event calling so often..

Thank you!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  1. Unfortunately no.
  2. Yes, the IntersectionObserver API is a very good and performant solution for this specific issue.

The API allows you to observe elements when they enter or leave the viewport. It does this away from the main thread, so you won't get any render blocking code.

In your specific case, observe the <header> element. Whenever the element leaves the view is the same point when the navbar becomes sticky. In the callback check if the isIntersecting property is true or false. This property indicates if the observed element is (partially) in view or not.

const header = document.querySelector('#header');
const nav = document.querySelector('#navbar');

const callback = ([entry]) => {
  const { isIntersecting } = entry;
  nav.classList.toggle('sticky', !isIntersecting);
};

const options = {
  root: null,
  rootMargin: '0px',
  threshold: 0
};

const observer = new IntersectionObserver(callback, options);
observer.observe(header);
* {
  margin: 0;
  text-align: center;
}

body {
  background: lightgrey;
}

header h1 {
  padding: 40px;
}

nav {
  position: sticky;
  top: 0;
  padding: 12px;
  background: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 255, 0, 0.5), rgba(255, 0, 0, 0.5), rgba(0, 0, 0, 0));
  color: black;
}

nav:before {
  content: "now bg transparent";
}

.container {
  min-height: 1000px;
  padding: 20px;
}

nav.sticky {
  background: linear-gradient(to right, rgb(0, 255, 0), rgb(255, 0, 0));
  color: white;
}

nav.sticky:before {
  content: "now bg isn't transparent";
}
<body>
  <header id="header">
    <h1>Header, that ask u to scroll page</h1>
  </header>
  
  <nav id="navbar"></nav>
  
  <div class="container">
    When we scroll page, and nav reached top of screen, .sticky add to classList<br>
  </div>
</body>

about 4 years ago · Santiago Trujillo 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!