Nav bar farb wechsel?

Hi ich möchte gerne das meine navbar die farbe wechselt wenn ich scrolle, was ich auch geschafft habe aber ich möchte gerne das sie erst die farbe wechselt wenn ich bis zu einer bestimmten höhe scrolle, also nicht direkt wenn ich anfange zu scrollen. Was muss ich da tun? :))) Hoffe man versteht was ich meine

<script>
 document.addEventListener('scroll',() => {
    const header =document.querySelector('header');
    
      if (window.scrollY >4) {
      header.classList.add('scrolled');
    } else {
      header.classList.remove('scrolled');
    }
  })
</script>
(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
verreisterNutzer
1 year ago
document.addEventListener('scroll', () => {
  const header = document.querySelector('header');
  
  if (window.scrollY > 100) {
header.classList.add('scrolled');
  } else {
header.classList.remove('scrolled');
  }
})

In the If query, you simply insert the number of pixels that need to be scrolled until it changes. In the example now 100.