0

What is the best way to run a JavaScript function when a user reaches a certain point on a web page?

Sébastien
  • 10,675
  • 11
  • 47
  • 67
Sean
  • 31
  • 3

1 Answers1

0

Check the offset of the document with document.documentElement.scrollTop

let called = false
document.addEventListener('scroll', e => {
  if (document.documentElement.scrollTop >= 500) {
    if (called) return
    called = true
    calledEvent()
  }
})

function calledEvent() {
  console.log('hello')
}
body {
  height: 1000px;
}
Get Off My Lawn
  • 27,770
  • 29
  • 134
  • 260