在div滚动上动态检查div的offsetTop

时间:2018-09-19 17:04:06

标签: javascript scroll offset addeventlistener

我正在尝试在uswr在div中滚动50px时提醒消息,但它不起作用:

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

这是怎么回事? codepen demo

1 个答案:

答案 0 :(得分:1)

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