(Jquery) Slider - arrows do not take into account what image is

0

I'm trying to create a jquery slider, I started creating the arrows backwards and forwards and they worked fine, then I wanted to create the buttons below so the user can click and go to the image he wants, and they work well , but my problem is that when I give a button below and then I want to use the arrows, this does not know what image is and does not work, and the same happens if I use the arrows and then I want to give a button below

$(document).ready(function(){
slider(500);
function slider(velocidad){

 var x=1;
 var numImg= $(".grande img").length;
 var ultimoMargin= -(numImg-1)*1000+"px";

$(".grande").css("width", numImg * 1000 + "px");

$(".next").click(function(){
    $(".grande").animate({"margin-left":"+=-1000"},velocidad);
    x++;

    if(x == numImg){
        $(".grande").animate({"margin-left":"0px"},0);
        x = 1;
    }

});

$(".prev").click(function(){

    if(x == 1){
        $(".grande").animate({"margin-left": ultimoMargin},0);
        x = numImg;
    }

    $(".grande").animate({"margin-left":"+=1000"},velocidad);
    x--;
});

$(".boton").click(function(){
     paginationPos = $(this).index() + 1;
            console.log(paginationPos);


            $("img").hide();
            $("img:nth-child("+ paginationPos +")").fadeIn();

        });
    }
});

Code with html and css link

    
asked by Alejandro25 08.10.2017 в 15:01
source

1 answer

0

You can try using the data in the HTML giving it a numerical value.

JS:

$(".boton").click(function(){

    var number = $(this).data("number");
    var calcx = number * 1000;

    $(".grande").animate({marginLeft : "-" + calcx});

});

HTML:

<div class="botones">
    <span data-number="0" class="boton"></span>
    <span data-number="1" class="boton"></span>
    <span data-number="2" class="boton"></span>
    <span data-number="3" class="boton"></span>
</div>

Example: link

    
answered by 08.10.2017 в 23:51