vuvu
vuvu

Reputation: 5338

Scroll event is not firing on my div element

I want to track a scroll of a div, but it does not work. What is wrong?

$('#box').on('scroll', function(){
    alert(33);
});

jsFiddle

Upvotes: 2

Views: 265

Answers (2)

aleks korovin
aleks korovin

Reputation: 744

You should add a height and overflow property to your div to make 'scroll' event work.

#box {
    overflow: auto;
    height: 200px;
}

JSFiddle example

Upvotes: 1

Rory McCrossan
Rory McCrossan

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;
}

Updated fiddle

Upvotes: 1

Related Questions