Search code examples
jqueryjquery-pluginssliderjquery-backstretch

Backstretch jQuery dot navigation


I am trying to build navigation for a backstretch slider using dots.

Currently I am using the value of the dot to change to the slide with that value on click.

Letting it autoplay 1 or 2 slides and the clicking on the first dot the index returns 01 instead of 0. This then breaks the autoplaying slider as well as the navigation.

I can't seem to find any fixes after googling, any ideas?

$(document).ready(function(){
	$(".slider").backstretch([
	"http://lw.dev.plx.mk/wp-content/uploads/2016/10/iStock_000015135168Large.jpg",
	"http://lw.dev.plx.mk/wp-content/uploads/2016/10/IMG_2961.jpg",
	"http://lw.dev.plx.mk/wp-content/uploads/2016/10/Global-Legend-White-conservatory-1-bespoke-external.jpg",
	"http://lw.dev.plx.mk/wp-content/uploads/2016/10/Buckley-3.jpg",		],{
		duration:3000,
		fade:750,
	});
	$('#dot-0').addClass('selected');

	$('.dot').click(function() {
	    $('.slider').backstretch("show", ($(this).attr('value')));
	    console.log('Value - ' + ($(this).attr('value')));
	    $('.dot').removeClass('selected');
	    $(this).addClass('selected');
	});

	$('.slider').on("backstretch.before", function (e, instance, index) {
		console.log('Index - ' + index);
		var currentSlide = $('#dot-' + index);
		$('.dot').removeClass('selected');
		$(currentSlide).addClass('selected');
	});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-backstretch/2.0.4/jquery.backstretch.min.js"></script>
<div class="slider" style="position: relative; z-index: 0; background: none;">
		<div class="slider-dots">
			<div class="dot" id="dot-0" value="0">x</div>
			<div class="dot" id="dot-1" value="1">x</div>
			<div class="dot" id="dot-2" value="2">x</div>
			<div class="dot" id="dot-3" value="3">x</div>
		</div>
</div>


Solution

  • 	$('.dot').first().addClass('selected');
    	
    	var instance = $(".slider").data("backstretch");
    	$('.dot').click(function () {
    	  var index = $('.dot').index( $(this) );
    	  $('.dot').removeClass('selected');
    	  $(this).addClass('selected');
    	  instance.show(index);
    	  return false;
    	});
    	
    	$('.slider').on("backstretch.before", function (e, instance, index) {
    		var currentSlide = $('#dot-' + index);
    		$('.dot').removeClass('selected');
    		$(currentSlide).addClass('selected');
    	});

    Fixed with the code above