0

I'm trying to alert message when uswr scroll 50px in div but it doesn't work:

function handleScroll() {
  console.log('scrolling... ', this.offsetTop);
  if(this.offsetTop > 50) alert('scrolled more than 50px!');
}
document.getElementById('a').addEventListener('scroll', handleScroll);

What's going on? codepen demo

1 Answer 1

1
function handleScroll() {
 alert('scrolling... '+ this.scrollTop);
  if(this.scrollTop > 50) alert('scrolled more than 50px!');
 }
document.getElementById('a').addEventListener('scroll', handleScroll);
Sign up to request clarification or add additional context in comments.

1 Comment

offset top just tell the co-ordinate of element vertically but scroll top tells the exact amount scrolled. up vote if you get this..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.