简体   繁体   中英

querySelector event listener `scroll`

I am trying to understand why scroll event listener of querySelector element doesn't work here

 document.querySelector('.test-scroll') .addEventListener('scroll', function() { alert('Scroll'); }, false); 
 .test-scroll { height: 2000px; overflow-x: none; overflow-y: scroll; border: 1px solid #000; } 
 <div class="test-scroll">&nbsp;</div> 

Because there is no content force it to actually scroll anything.

Here I added an inner div , which is higher, and make it scrollable.

Stack snippet

 document.querySelector('.test-scroll') .addEventListener('scroll', function() { alert('Scroll'); }, false); 
 .test-scroll { height: 2000px; overflow-x: none; overflow-y: scroll; border: 1px solid #000; } .test-scroll div { height: 3000px; background: red; } 
 <div class="test-scroll"> <div></div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM