javascript - jQuery carousel - any number of images -
i have flickity carousel of images that, when click image/slide, opens modal window show zoomed in version of said image. within modal have left , right buttons scroll through zoomed in images.
my problem code breaks if have more or less 3 images in slider. need convert not break no matter how many images have, whether 1, 4, 2 or 8 etc. how do this?
my js below has been reduced show relevant bits. have function right button too. see jsfiddle current situation: https://jsfiddle.net/293sh54n/6/
js
var activedivimg; $( ".product--slider .slide" ).each(function( index ) { // show modal window $('#product-slider-flickity img').click(function(){ activedivimg = parseint($(this).parent().index()); console.log(activedivimg) $("#imageshow").html('<img src="'+$(this).attr('src')+'"/>'); $(".p-image-zoom").addclass("md-show"); }); }); // zoomed in controls - scroll left button $('.left').click(function(){ if(activedivimg < 1 ){ activedivimg = 2 }else{ console.log('kundi'+activedivimg) activedivimg = activedivimg - 1 } console.log('left'+activedivimg) $("#imageshow").html('<img src="'+$('.slide').eq(activedivimg).find('img').attr('src')+'"/>'); console.log(activedivimg+"asd") });
first of should images count. after in left click use activedivimg = imagescount - 1; instead of activedivimg = 2; same thing right click. fiddle
var imagecount = $('.slide').length; var activedivimg; $( ".product--slider .slide" ).each(function( index ) { // show modal window $('#product-slider-flickity img').click(function(){ activedivimg = parseint($(this).parent().index()); console.log(activedivimg) $("#imageshow").html('<img src="'+$(this).attr('src')+'"/>'); $(".p-image-zoom").addclass("md-show"); }); }); // zoomed in controls - scroll left button $('.left').click(function(){ if(activedivimg < 1 ){ activedivimg = imagescount - 1; }else{ console.log('kundi'+activedivimg) activedivimg = activedivimg - 1 } console.log('left'+activedivimg) $("#imageshow").html('<img src="'+$('.slide').eq(activedivimg).find('img').attr('src')+'"/>'); console.log(activedivimg+"asd") });
Comments
Post a Comment