Last active
May 11, 2016 22:13
-
-
Save dariye/eadf4e12dc386698f8a096f1858e7a9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Inspired by http://g-liu.com/blog/2013/08/tutorial-basic-carouselslideshow-with-javascript/ | |
var SimpleCarousel = function(id, options){ | |
this.id = id || 'slides'; // default id -> #slides | |
this.class = options.class || 'slide'; // default child class -> .slide | |
this.w = options.width; | |
this.h = options.height; | |
this.duration = options.duration || 4000; // default 4s | |
this.container = document.getElementById(this.id); | |
this.carouselItems = document.getElementsByClassName(this.class); | |
/** | |
* Array to store carousel items | |
* @name items | |
* @type array | |
*/ | |
this.items = this.carouselItems; | |
this._init(); | |
} | |
SimpleCarousel.prototype._init = function(){ | |
var self = this; | |
setInterval(function(){ | |
self.switch(); | |
}, 4000); | |
} | |
SimpleCarousel.prototype.switch = function(){ | |
var self = this; | |
var items = self.items; | |
console.log(items); | |
var activeItem = getActiveItem(self.items); | |
var nextItem = next(activeItem, self.items.length); | |
self.items[activeItem].style.display = 'none'; | |
self.items[nextItem].style.display = 'table'; | |
function getActiveItem(){ | |
activeItem = -1; | |
for (var i=0; i < self.items.length; i++){ | |
if(self.items[i].style.display == 'table') { | |
activeItem = i; | |
} | |
} | |
return activeItem; | |
} | |
function prev(num, length){ | |
if(num == 0) return length - 1; | |
else return num - 1; | |
} | |
function next(num, length){ | |
if(num == length - 1) return 0; | |
else return num + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
HTML
CSS