James Knight James Knight - 4 months ago 10
Javascript Question

Carousel not working? JQUERY

This is my code that I need for my carousel. I do not know why it is not working. I have ran the function in another line of code. Maybe there is some other error. This carousel is supposed to have 4 different content pages, and they are all supposed to fade in or fade out.

var main = function() {
$('.dropdown-toggle').click(function() {
$('.dropdown-menu').toggle();
});
$('.arrow-next').click(function() {
var currentSlide = $('.active-slide');
var nextSlide = currentSlide.next();

var currentDot = $('.active-dot');
var nextDot = currentDot.next();

if(nextSlide.length === 0) {
nextSlide = $('.slide').first();
nextDot = $('.dot').first();
}

currentSlide.fadeOut(300).removeClass('active-slide');
nextSlide.fadeIn(300).addClass('active-slide');

currentDot.removeClass('active-dot');
nextDot.addClass('active-dot');
});
$('.arrow-prev').click(function() {
var currentSlide = $('.active-slide');
var prevSlide = currentSlide.prev();

var currentDot = $('.active-dot');
var prevDot = currentDot.prev();

if (prevSlide.length === 0) {
prevSlide = $('.slide').last();
prevDot = $('.dot').last()
}
});
};

Answer

You will need this code for it to work. Add it in the var main function but outside .arrow-prev, .arrow-next, or dropdown-toggle.

currentSlide.fadeOut(300).removeClass('active-slide');
prevSlide.fadeIn(300).addClass('active-slide');
currentDot.removeClass('active-dot');
prevDot.addClass('active-dot');