I need a standalone JavaScript onscroll
event handler (without frameworks such as jQuery, Prototype, mootools, etc.), which is also cross browser.
I have searched for one, I can only find thousands of examples using jQuery or Prototype.
The onscroll event occurs when an element's scrollbar is being scrolled. Tip: use the CSS overflow style property to create a scrollbar for an element.
The scroll event does not bubble up. Although the event does not bubble, browsers fire a scroll event on both document and window when the user scrolls the entire page.
To detect if a user is scrolling with JavaScript, we can watch for scroll events by adding event handlers. to add the userHasScrolled variable. Then we set the window. onscroll property to a function that runs when we scroll.
From element.addEventListener:
function onScrollEventHandler(ev)
{
alert(ev);
//http://dev.w3.org/2006/webapi/DOM-Level-3-Events/html/DOM3-Events.html#event-type-scroll
}
var el=window;
if(el.addEventListener)
el.addEventListener('scroll', onScrollEventHandler, false);
else if (el.attachEvent)
el.attachEvent('onscroll', onScrollEventHandler);
See it on MDN:
https://developer.mozilla.org/en/DOM/window.onscroll
window.onscroll = function (e) {
// called when the window is scrolled.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With