I created this module, and I'm wondering if I did a good job. How would an experienced JavaScript developer improve this simple slider module further? Like, let's say that you want to use this module on 3 sliders on the same page.
Here is a jsFiddle.
Module
var sliderTest = (function(){
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
✚ private variables
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
var expoElement = document.querySelector(".test")
var expoCanvas = expoElement.querySelector(".canvas")
var expoSlides = expoCanvas.querySelectorAll(".slide")
var controlPrev = expoElement.querySelector(".control.prev")
var controlNext = expoElement.querySelector(".control.next")
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
✚ private functions
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
function whereIsActive(){
var y = {}
for(var i = 0; i < expoSlides.length; i++){
if(expoSlides[i].classList.contains("active")){
y.current = i;
if(expoSlides[i+1]){
y.next = i+1;
} else {
y.next = 0;
}
if(expoSlides[i-1]){
y.prev = i-1;
} else {
y.prev = expoSlides.length-1;
}
}
}
return y
}
function removeActiveClasses(){
for(var i = 0; i < expoSlides.length; i++){
expoSlides[i].classList.remove("active")
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
✚ public methodes
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
var publicMethod = {
nextSlide:function(ev){
ev.preventDefault();
var nextIndex = whereIsActive().next;
removeActiveClasses();
expoSlides[nextIndex].classList.add("active");
},
prevSlide:function(ev){
ev.preventDefault();
var prevIndex = whereIsActive().prev;
removeActiveClasses();
expoSlides[prevIndex].classList.add("active")
},
sendInTheClone:function(){alert(whereIsActive().current)}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
✚ event listners
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
controlNext.addEventListener("click", publicMethod.nextSlide)
controlPrev.addEventListener("click", publicMethod.prevSlide)
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
✚ return
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
return publicMethod;
})();
HTML
<div class="test">
<div class="canvas">
<div class="slide ">1</div>
<div class="slide ">2</div>
<div class="slide active">3</div>
<div class="slide ">4</div>
</div>
<a class="control prev" href="" data-control="prev">prev</a> <a class="control next" href="" data-control="next">next</a>
</div>