views:

148

answers:

2

Does a div element not have , an onscroll event handler ? The behaviour on my page, doesn't seem to indicate the div onscroll eventHandler is recognized.

<div id='bd' onscroll='alert('Scroll Called');'></div>.

Also,
Do div scroll events roll up to window scroll events, as per DOM event bubbling ?

+1  A: 

Yes but the element needs to have a scrollbar. You can give it one with either overflow: auto (which gives you a scrollbar when the content is tall enough) or overflow: scroll. (These can be set specifically for x & y as well overflow-y: scroll...)

Although they don't bubble once the div has been scrolled to the bottom the window will start scrolling. (Basically if the div can scroll it will intercept the scroll event, but if it can't then it will go to the page)

Caleb
Thanks. But, i have the overflow property set. I am scrolling the div, but not getting any alert. What may i be doing wrong ?
The Machine
A: 

I know it may not be exactly what you're looking for, but a lot of javascript frameworks can help you with this. It is not necessary for the div to have a scrollbar for you to hook to the scroll events.

Eg. Mootools has the mousewheel event. Demo here. (It has scrollbars, but you can use Firebug to remove the scrollbars and try -- it still works).

I have used this myself on a site I made a while back. If you scroll while holding your mouse over the images it prevents the default page scrolling and instead slides the image-bar.

mqchen