Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Scroll event listener not working

Scroll event listener not working

Ric737
Visitor
1 0 0
window.addEventListener('scroll', function () {
  var header = document.getElementsByTagName('header')[0];
  var firstSection = document.querySelector('shopify-section-sections--18210351677669__header'); // First section that determines when to change

  if (window.scrollY > firstSection.offsetHeight) {
    header.style.backgroundColor = 'black';
  } else {
    header.style.backgroundColor = '';
  }
});

I am trying to initiate this JS function on scroll which turns the background of the header into black when it passes the first section of the webpage. I have even tried using setTimeInterval to achieve the same functionality but even then the function is not run. The function is added in globals.js,

 

What am I missing here?

Replies 0 (0)