A small jQuery extension that oscillates integer values incrementally between a min and max.
-
options Optional hash of defaults: min, max, speed, start & dir
-
function Optional function to be called.
Include script after the jQuery library (unless you are packaging scripts somehow else):
<script src="/path/to/jquery.oscillator.js"></script>
Let's say you want to continuously oscillate the opacity of the body of a webpage, let's create a new oscillator and pass a function to update our body CSS:
var myOscillator = new $.oscillator({ min: 0, max: 100, speed: 50 }, function() {
$('body').css({ opacity: myOscillator.value/100 });
});
By default, the oscillator will begin oscillating. To pause:
myOscillator.pause();
To restart:
myOscillator.start();
Or you can toggle between starting and pausing:
myOscillator.toggle();
To stop the oscillator and reset the options to the initial state:
myOscillator.stop();
At any point you can grab the current value of the oscillator:
myOscillator.value;
You can pass a hash of options, with the defaults set to:
options = {
start: true, // begin oscillations on init
min: 0, // minimum value
max: 100, // maximum value
speed: 1000, // increment speed (ms)
value: null, // initial value
increment: 1, // integer value to increment by
direction: 'up' // initial increment direction: 'up' or 'down'
}