Skip to content

Instantly share code, notes, and snippets.

@hrumhrumble
Created November 30, 2014 17:20
Show Gist options
  • Save hrumhrumble/3b1067775442a14614ca to your computer and use it in GitHub Desktop.
Save hrumhrumble/3b1067775442a14614ca to your computer and use it in GitHub Desktop.
// simple slider
function Slider (slider, duration, startSlide) {
this.slider = document.getElementById(slider);
this.slides = this.slider.getElementsByTagName('div');
this.duration = duration || 1000;
this.startSlide = startSlide || 0;
this.init();
}
Slider.prototype.info = function() {
console.log(
"[ duration: " + this.duration + "]\n",
"[ slider count: " + this.slides.length + "]\n",
"[ start slide: " + this.startSlide + "]\n"
);
};
Slider.prototype.init = function() {
this.showSlide();
};
Slider.prototype.next = function() {
if (this.startSlide >= this.slides.length - 1) {
this.startSlide = 0;
} else {
this.startSlide++;
}
this.showSlide();
};
Slider.prototype.prev = function() {
if (this.startSlide <= 0) {
this.startSlide = this.slides.length - 1;
} else {
this.startSlide--;
}
this.showSlide();
};
Slider.prototype.showSlide = function() {
for (var i = 0; i < this.slides.length; i++) {
this.slides[i].className = "slide";
}
this.slides[this.startSlide].className += " visible";
};
Slider.prototype.autoplay = function() {
var that = this;
setInterval(function() {
that.next();
}, this.duration)
};
window.onload = function() {
var slider = new Slider('slider', 2000);
slider.autoplay();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment