Reputation: 5338
I want to track a scroll of a div, but it does not work. What is wrong?
$('#box').on('scroll', function(){
alert(33);
});
Upvotes: 2
Views: 265
Reputation: 744
You should add a height and overflow property to your div to make 'scroll' event work.
#box {
overflow: auto;
height: 200px;
}
Upvotes: 1
Reputation: 337701
The issue is because the #box
element is not being scrolled in your example - the window
is. By default block elements will expand to fit their contents. To stop this behaviour and allow the element to scroll you need to set its height
and overflow
properties:
#box {
height: 100px; /* amend this as required */
overflow: scroll;
}
Upvotes: 1