scroll-affair
v1.0.4
Published
A simple JavaScript library for not binding your magic directly to the window.onscroll event.
Downloads
8
Maintainers
Readme
ScrollAffair
A simple JavaScript library for not binding your magic directly to the window.onscroll event.
About
You almost never want to bind a function directly to the window.onscroll event, since it's a big performance killer. ScrollAffair makes it easy to set a maximum execution frequency of your function when the user is scrolling.
Installation
ScrollAffair is available as a Bower or npm package:
Bower
$ bower install scroll-affair --save
npm
$ npm install scroll-affair --save
Example
This example will recalculate the window.scrollY every 80ms when the user is scrolling instead of every time the browser fires the window.onscroll event (which is a lot):
var scrollY = window.scrollY,
scrollAffair = new ScrollAffair({
callback: recalculateScrollY, // Callback function to bind to the scroll event
frequency: 80, // Frequency for executing callback function in milliseconds. Default is 80ms.
animationFrame: false // Boolean whether to use requestAnimationFrame for callback function. Default is false.
});
function recalculateScrollY() {
scrollY = window.scrollY;
}