Search code examples
javascriptreactjsfunctionscrollsafari

window.onscroll arrow function not working in Safari


I'm trying to detect window position on scroll but the window.onscroll = () => {} function doesn't work in Safari, although it works fine in Chrome and Firefox.

  myFunction() {
      window.onscroll = () =>{
        if(someCondition){
          ...do something
        } 
      }
  }

For some reason, Safari is the only browser where I can't log anything inside the window.onscroll function. I'm binding the function in the constructor and attaching it to a scroll event when the component mounts.

Does anyone know why this might be happening?


Solution

  • Following script works from me on safari. Please try.

    document.addEventListener('scroll',()=>{
      console.log('scrolling')
    });
    body {
    height: 1200px;
    background: blue;
    }
    <div>Scroll</div>