I've created a slideshow using jQuery Cycle 2 and the Carousel plugin. So far so good, everything works as it should, it's even fluid. But I would like it to be responsive as well.
I found a script on the official Github by a user, but the thread is not active anymore. I tried to edit it myself, but without luck so far.
The problem is that script checks the viewport and based on the viewport shows a certain amount of slides. This works, when I refresh the page. However, when I resize the page manually, the script goes bezerk and I don't know why. The difference is made by carouselVisible: $ The problem is in initCycle(); (at least that is what I expect).
The JS is (complete Fiddle below):
// Responsive Slideshow
function initCycle() {
var width = $(window).width();
if (width < 768) {
$(".slideshow").cycle({
manualSpeed: 500,
timeout: 0,
slides: 'li',
next: '#next4',
prev: '#prev4',
pagerActiveClass: 'selected',
pagerTemplate: '',
fx: 'carousel',
carouselVisible: 2,
carouselOffset: 20,
carouselFluid: true
});
} else if (width > 768 && width < 980) {
$(".slideshow").cycle({
manualSpeed: 500,
timeout: 0,
slides: 'li',
next: '#next4',
prev: '#prev4',
pagerActiveClass: 'selected',
pagerTemplate: '',
fx: 'carousel',
carouselVisible: 3,
carouselOffset: 20,
carouselFluid: true
});
} else {
$(".slideshow").cycle({
manualSpeed: 500,
timeout: 0,
slides: 'li',
next: '#next4',
prev: '#prev4',
pagerActiveClass: 'selected',
pagerTemplate: '',
fx: 'carousel',
carouselVisible: 4,
carouselOffset: 20,
carouselFluid: true
});
}
}
initCycle();
function reinit_cycle() {
var width = $(window).width();
if (width < 768) {
$('.slideshow').cycle('destroy');
reinitCycle(2);
} else if (width > 768 && wWidth < 980) {
$('.slideshow').cycle('destroy');
reinitCycle(3);
} else {
$('.slideshow').cycle('destroy');
reinitCycle(4);
}
}
function reinitCycle(visibleSlides) {
$(".slideshow").cycle({
manualSpeed: 500,
timeout: 0,
slides: 'li',
next: '#next4',
prev: '#prev4',
pagerActiveClass: 'selected',
pagerTemplate: '',
fx: 'carousel',
carouselVisible: visibleSlides,
carouselFluid: true
});
}
var reinitTimer;
$(window).resize(function () {
clearTimeout(reinitTimer);
reinitTimer = setTimeout(reinit_cycle, 100);
});