Is it possible to fire a specific JavaScript event when a certain DIV comes into view on the page?
Say, for example, I have a very large page, like 2500×2500 and I have a 40×40 div that sits at position 1980×1250. The div is not necessarily manually positioned, it could be there due to the content pushing it there. Now, is it possible to run a function when the user scrolls to a point where the div becomes visible?
Not automatically. You would have to catch scroll events and check for it being in view each time by comparing the co-ordinates of the div rectangle with the visible page rectangle.
Here’s a minimal example.
You could improve this by:
onscrollon all ancestors that haveoverflowscrollorautoand adjusting the top/left co-ords for their scroll positionsoverflowscroll,autoandhiddencropping putting the div off-screenaddEventListener/attachEventto allow multiple VisibilityMonitors and other things using the resize/scroll eventsgetElementRectto make the co-ords more accurate in some cases, and some event unbinding to avoid IE6-7 memory leaks, if you really need to.