Javascript 在div滚动上动态检查div的偏移量

Javascript 在div滚动上动态检查div的偏移量,javascript,scroll,offset,addeventlistener,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); 发生什么

当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);

发生什么事了

偏移顶部只告诉元素垂直坐标,但滚动顶部告诉确切的滚动量。如果你得到这个,请投票。。
function handleScroll() {
 alert('scrolling... '+ this.scrollTop);
  if(this.scrollTop > 50) alert('scrolled more than 50px!');
 }
document.getElementById('a').addEventListener('scroll', handleScroll);