function Slides(){ this.block = false; this.currentIndex = 0; this.slides = []; this.$el = null; this.debug = false; this.animationTime = parseInt(2000) | 1500; this.onArrows = null; this.arrows = function(){ var arrowL = $("
").addClass("sliderArrow").addClass("left"); var arrowR = $("
").addClass("sliderArrow").addClass("right"); var self = this; arrowR.click(function(){ self.showNext(); if(self.onArrows != null) self.onArrows(); }); arrowL.click(function(){ self.showPrev(); if(self.onArrows != null) self.onArrows(); }); this.$el.append(arrowL); this.$el.append(arrowR); } this.init = function(el){ this.$el = $(el); var children = $(el).children(); if(this.debug) console.log("INIT", el, children); var self = this; children.each(function(index, child){ var $child = $(child); self.slides.push($child); //$child.css({left:"100%"}); self.slideRight(child, true); }); this.arrows(); this.slideIn(this.slides[0], true); } this.show = function(idx, immediate){ if(this.block) return; var idx = Math.max(0, idx); var idx = Math.min(this.slides.length-1, idx); if(this.currentIndex == idx) return; if(idx < this.currentIndex) this.slideRight(this.slides[this.currentIndex], immediate); else this.slideLeft(this.slides[this.currentIndex], immediate); this.slideIn(this.slides[idx], immediate); var idx0 = Math.min(idx,this.currentIndex); var idx1 = Math.max(idx,this.currentIndex); for(var i = idx0 + 1; i < idx1; i++){ var slide = this.slides[i]; if(i < idx) this.slideLeft(slide, true); else if(i > idx) this.slideRight(slide, true); } this.currentIndex = idx; } this.showNext = function(){ var nextIdx = this.currentIndex + 1; if(nextIdx >= this.slides.length) nextIdx = 0; this.show(nextIdx); } this.showPrev = function(){ var nextIdx = this.currentIndex - 1; if(nextIdx < 0) nextIdx = this.slides.length - 1; this.show(nextIdx); } this.slide = function(left){ if(left) this.slideLeft(); else this.slideRight(); } this.slideIn = function(el, immediate){ if(immediate){ $(el).css({left:"0%"}); return; } this.block = true; var self = this; $(el).animate({left: "0%"}, this.animationTime, function(){ self.block = false; }); } this.slideLeft = function(el, immediate){ if(immediate){ $(el).css({left: "-100%"}); return; } this.block = true; var self = this; $(el).animate({left: "-100%"}, this.animationTime, function(){ self.block = false; }); } this.slideRight = function(el, immediate){ if(immediate){ $(el).css({left: "100%"}); return; } this.block = true; var self = this; $(el).animate({left: "100%"}, this.animationTime, function(){ self.block = false; }); } } $(function(){ var slides = new Slides(); slides.debug = false; var el = $(".slides").get(0); var autoRotate = $(".slides").attr("auto-rotate") == "true"; var slide = parseInt($(".slides").attr("slide")); slides.init(el); var delay = parseInt(5000) | 1500; var intId = -1; if(!isNaN(slide)){ slides.show(slide, true); } if(autoRotate){ intId = setInterval(function(){ slides.showNext(); }, delay); slides.onArrows = function(){ clearInterval(intId); intId = setInterval(function(){ slides.showNext(); }, delay); } } });