What is the best way to run a JavaScript function when a user reaches a certain point on a web page?
Asked
Active
Viewed 47 times
1 Answers
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;
}
![](../../users/profiles/1778465.webp)
Get Off My Lawn
- 27,770
- 29
- 134
- 260