Data Slider //JavaScript Repository

Description

Auto rotates through objects.
Created: 2005.08.08

Code (Download)

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/classes/data-slider [rev. #1]

(DataSlider = function(onchange, interval, args){
    var i = DataSlider.instances = DataSlider.instances || [], o = this;
    (o.c = 0, o.timer = null, o.interval = (o.onchange = (o.data = [].slice.call(
        arguments, 0)).shift(), o.data.shift()), i[o.index = i.length] = o);
}).prototype = {
    stop: function(){
        clearTimeout(this.timer);
    },
    play: function(){
        this.timer = setInterval("DataSlider.instances[" + this.index + "].next()", this.interval);
    },
    show: function(x){
        this.c = x; this.onchange(this.data[ x ]);
    },
    previous: function(){
        this.show(this.c > 0 ? --this.c : this.data.length-1);
    },
    next: function(){
        this.show((this.c + 1) % this.data.length);
    }
};

Example (Example)

<form action="">
    <fieldset>
    <legend>Rotacionador de objetos</legend>
    <div id="dados">:]</div>
    <input type="button" name="stop" value="parar" />
    <input type="button" name="previous" value="anterior" />
    <input type="button" name="next" value="pr?ximo" />
    <input type="button" name="play" value="play" />
    </fieldset>
</form>

<script type="text/javascript">
//<![CDATA[

function handler(data){
    var x = document.getElementById("dados");
    x.innerHTML = '<a href="' + data[1] + '">'+ data[0] +"</a>";
}

var x = new DataSlider(handler, 1000, ["ABCDE", "http://www.abcde.com"], ["FGHIJ", "http://www.fghij.com.br"], ["KLMNO", "http://www.klmno.com"]);
x.play();

var f = document.forms[0];
//http://www.jsfromhell.com/geral/event-listener
addEvent(f.play, "click", function(){x.play();});
addEvent(f.stop, "click", function(){x.stop();});
addEvent(f.next, "click", function(){x.stop(); x.next();});
addEvent(f.previous, "click", function(){x.stop(); x.previous();});

//]]>
</script>

Help

Constructor

DataSlider(onchange: Function(data: Object): void, interval: Integer, arg0..argN: Object)
Generates an instance of DataSlider.
onchange
function that will be called each time the class advances to the next item
interval
time interval in milisegundos that controls when the item should be rotated
arg0..argN
objects that will be added to the container

Properties

DataSlider.onchange: Function(data: Object): void
function that will be called each time the class advances to the next item
DataSlider.interval: Integer
time interval in milisegundos that controls when the item should be rotated

Methods

DataSlider.play(void): void
Starts the automatic sliding.
DataSlider.stop(void): void
Interrupts the automatic sliding.
DataSlider.show(index: Integer): void
Selects an object and calls the onchange event passing itself as argument.
index
index of the object
DataSlider.previous(void): void
Goes to the next object.
DataSlider.next(void): void
Goes to the previous object.

Rank (Votes: 21)

2.29