I have a Bootstrap 4 carousel and I need to move 4 slides each time. I’m using this code, and it works:
$(this).carousel(4);
The problem is that the carousel just jumps to that index and doesn’t show any transitions. If I use $(this).carousel("next");
or $(this).carousel("prev");
it works and transitions correctly.
I tried use something like this:
$(this).carousel("next");
$(this).carousel("next");
But it does not work. Any help will be appreciated.
UPDATE
I found a workaround:
test = setInterval(() => {
count++;
$(".carousel-control-next").click();
if (count > 3) {
count = 0;
clearInterval(test);
}
}, 200);
I used an interval because the following does not work:
$(".carousel-control-next").click()
inside a loop, or$(".carousel-control-next").click()
inside asetTimeout
inside a loop
2
Answers
first of all you need to include the required JavaScript and CSS in your project, create the necessary HTML and use build in carousel class.
link in the "head" and scripts in the end of "body".
Then create the Carousel HTML using the carousel class and a unique IDs. The structure should include indicators, items, and nav controls:
Swap "img1.jpg", "img2.jpg", and "img3.jpg" with the your image paths or links (urls) you want to use.
And last thing, activate the carousel, bootstrap automatically activates the it when the
data-ride="carousel"
attribute is added to the main Carouseldiv
element.Also you can check more info here: carousel bootstrap docs.
Hope this helps and answers your question.
Almost forgot:
To enable smooth transition effect modify code:
I am not sure why
.carousel(4)
doesn’t animate for you, it does on a basic example:Two notes:
.carousel(4)
jumps to the slide at index 4 (i.e. the fifth slide), regardless of the current slide. If you want to jump 4 forward from the current slide, useThe animation goes directly from the current slide to the target slide, skipping over any intermediate slides. If you want the animation to go through all the intermediate slides, have a look at @David NoHorizon’s answer.
I noticed that calling
.carousel("next")
twice doesn’t work for me either (it only goes forward one slide).