Tiny listener on numerical value changes. Works in Node and with Browserify.
With npm do:
$ npm install downup --save
var Downup = require('downup');
// Create instance
var downup = new Downup();
// Add listener
downup.on(1, function(offset) {
console.log('Reached 1 and going %s', offset > 0 ? 'up' : 'down');
});
// Will trigger since default value is 0
downup.update(2);
// Will trigger again with offset -1
downup.update(0);
// Will not trigger
downup.set(2);
// Remove all listeners attached to number 1
downup.off(1);
// Will not trigger since the listener has been removed
downup.update(0);
// Update on scroll (please debounce this ;)
window.addEventListener('scroll', function(e){
downup.update(window.scrollY);
});
// Listen to menu and add sticky class
var menu = document.querySelector('#menu');
downup.on(menu.offsetTop, function(diff){
menu.classList[diff > 0 ? 'add' : 'remove']('stick');
});
downup extends TinyEmitter and inherit from its instance methods.
Subscribe to a number
number
- the number to subscribe tocallback
- the function to call when number is reachedcontext
- (OPTIONAL) - the context to bind the event callback to
Subscribe to an event only once
number
- the number to subscribe tocallback
- the function to call when number is reachedcontext
- (OPTIONAL) - the context to bind the event callback to
Unsubscribe from an number. If no callback is provided, it unsubscribes you from all callbacks registered to this number.
number
- the number to unsubscribe fromcallback
- the function used when binding to the number
Update downup.value
and trigger any registered numbers between old value and this one
number
- the new value
Similar to update(number)
but it will not trigger any event.
number
- the new value
Trigger a specific number (should not be used...)
number
- the number to emitoffset
- the offset to pass to the callbacks
Build (Tests, Browserifies, and minifies)
npm install
npm run build
Test
npm install
npm test
MIT