I need to change images in a slide of an image slider.
Every slide contains two images which will change after 3 seconds.
So, the slide comes with the first image, after 3 seconds it changes the first image to the second image and after another 3 seconds ( so 6 seconds total ) the current slide goes and the next slide comes.
My idea is to use setTimeOut
function and to call every 3 seconds another image.
So after 3 seconds fadeOut()
the first image and fadeIn()
the second.
I tried this using cycle2 and its cycle-after
and cycle-before
event:
$('.slide .center img:nth-child(2)').hide(); // hide all slides second images
$('#slides').cycle({
fx: 'scrollHorz',
slides: '>.slide',
timeout: 6000,
prev: '#prev',
next: '#next'
});
$('#slides').on('cycle-before', function(event, optionHash){
setTimeout(function(){
$('.slide:eq('+optionHash.currSlide+') > .center > img:first-child').fadeOut(1000);
console.log($('.slide:eq('+(optionHash.currSlide)+')').attr('slideNum'));
},1000)
setTimeout(function(){
$('.slide:eq('+optionHash.currSlide+') > .center > img:nth-child(2)').fadeIn();
console.log($('.slide:eq('+(optionHash.currSlide)+')').attr('slideNum'));
},2000)
});
$('#slides').on('cycle-after', function(event, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag) {
$('.center img:first-child',outgoingSlideEl).show();
$('.center img:nth-child(2)',outgoingSlideEl).hide();
});
But this doesn't work as I expected. It doesn't change the images and it breaks on next
and prev
button. If you see the console, you'll see that it doesn't trigger the current slide
.
Any help is appreciated.