1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| var carrousel = {
nbSlide : 0,
nbCurrent : 1,
elemCurrent : null,
elem : null,
init : function(elem){
this.nbSlide = elem.find('.slide').length;
//Créer la navigation
elem.append('<div class="navigation_num"></div>');
for(var i = 1;i<= this.nbSlide;i++){elem.find('.navigation_num').append('<span>'+i+'</span>');}
elem.append('<div class="navigation_btn"></div>');/* on crée la div navigation avec boutons flèches*/
elem.find(".navigation_btn").append('<span class="precedent"><--</span><span class="suivant">--></span>');/*on crée les span flèches*/
elem.find('.navigation_num span').click(function(){carrousel.gotoSlide($(this).text());})
elem.find(".navigation_btn .suivant").click(function(){carrousel.gotoNextImage(carrousel.nbCurrent+1);});
elem.find(".navigation_btn .precedent").click(function(){carrousel.gotoPreviousImage(carrousel.nbCurrent-1);});
//Initialisation du carrousel
this.elem = elem;
elem.find('.slide').hide();
elem.find('.slide:first').show();
this.elemCurrent = elem.find('.slide:first');
this.elem.find('.navigation_num span:first').addClass('active');
},
//Fonctions de navigation
//numéros
gotoSlide : function(num){
if(num == this.nbCurrent){return false;}
this.elemCurrent.hide();
this.elem.find('#slide'+num).show();
this.elem.find('.navigation_num span').removeClass('active'); /////////////////////////////
this.elem.find('.navigation_num span:eq('+(num-1)+')').addClass('active'); /////////////////////////////
this.nbCurrent = num;
this.elemCurrent = this.elem.find('#slide'+num);
},
//suivant
gotoNextImage : function (num){
if(num <= this.nbSlide) // on vérifie que l'on ne soit pas à la fin des images
{
this.elemCurrent.hide(); // quand on click, cache le slide en cours
this.elem.find("#slide"+ num).show(); // on va ensuite chopper le slide en fonction de son indice
this.elem.find('.navigation_num span').removeClass('active'); /*++++++++++++++*/
this.elem.find('.navigation_num span:eq('+(num-1)+')').addClass('active'); /*++++++++++++++*/
this.nbCurrent = num; // on met à jour l'indice de l'élement en cours
this.elemCurrent = this.elem.find("#slide"+num); // on met à jour l'elem en cours
}
else // sinon on retourne au slide 1
{
num=1;
this.elemCurrent.hide();
this.elem.find("#slide"+ num).show(); // on va ensuite chopper l'elem en fonction de son indice
this.elem.find('.navigation_num span').removeClass('active'); /*++++++++++++++*/
this.elem.find('.navigation_num span:eq('+(num-1)+')').addClass('active'); /*++++++++++++++*/
this.nbCurrent = num; // on met à jour l'indice de l'élement en cours
this.elemCurrent = this.elem.find("#slide"+num); // on met à jour l'elem en cours
}
},
//Precedent
gotoPreviousImage : function (num){
if(num<1) // si on veut aller, en dessous de 0, on retourne au dernier slide
{
num=this.nbSlide;
this.elemCurrent.hide(); // quand on click, cache le slide en cours
this.elem.find("#slide"+ num).show(); // on va ensuite chopper l'elem en fonction de son indice
this.elem.find('.navigation_num span').removeClass('active'); /*++++++++++++++*/
this.elem.find('.navigation_num span:eq('+(num-1)+')').addClass('active'); /*++++++++++++++*/
this.nbCurrent = num; // on met à jour l'indice de l'élement en cours
this.elemCurrent = this.elem.find("#slide"+num); // on met à jour l'elem en cours
}
else
{
this.elemCurrent.hide();
this.elem.find("#slide"+ num).show(); // on va ensuite chopper l'elem en fonction de son indice
this.elem.find('.navigation_num span').removeClass('active'); /*++++++++++++++*/
this.elem.find('.navigation_num span:eq('+(num-1)+')').addClass('active'); /*++++++++++++++*/
this.nbCurrent = num; // on met à jour l'indice de l'élement en cours
this.elemCurrent = this.elem.find("#slide"+num); // on met à jour l'elem en cours
}
},
}
$(function(){
carrousel.init($('#carrousel'));
}); |
Partager