0

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>

1
  • 1
    The scroll bar isn't in the DIV, it's in the main page. You'll only get a scroll bar in the DIV if its contents are too big to fit, but you only have one line of content. Commented Apr 7, 2018 at 6:54

1 Answer 1

4

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>

Sign up to request clarification or add additional context in comments.

Comments

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.