Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap carousel: How to slide two carousel sliders at a same time?

I have three carousel sliders on the single page and I want them to move two of them at the same time .i.e. both should change slider images at the same time. Both have same number of images/slides. Here is the code I am using:

jQuery('#carousel-example-generic1, #carousel-example-generic2').carousel({
    interval: 4000
});

And also I tried this code below:

jQuery('.carousel').carousel({
    pause:'false'
});

jQuery('#carousel-example-generic1').on('slide', function(){
    jQuery('#carousel-example-generic2').carousel('next');
});

But left and right sliders have very little delay in changing slides. And this delay goes on increasing. Any known issues with this kind of problem? Link to the site is this.

JSFiddle: Link

like image 526
atif Avatar asked Dec 20 '13 18:12

atif


People also ask

How do I put multiple carousel on one page?

To insert multiple jQuery carousels to the same webpage, you need to create each carousel with a unique ID. In Amazing Carousel, Publish dialog, select the option Publish to Folder, then click Browse to select a folder to save the generated files. You need to set up a unique Carousel ID for each carousel.

How do I create multiple carousel items?

We will add a class a row and inside it we will make a 12 columns class for containing our carousel. Inside that we will use how much item we want to show at once like if we want to show 3 items per slide then we will place each columns size to 4 so that 4+4+4=12.

How do I display Bootstrap carousel with three posts in each slide?

Following are the steps to create a Bootstrap carousel:Apply CSS to resize the . carousel Bootstrap card body by using the code segment below. In this step, the sliding images are defined in the division tag as under. Last step is to add controls to slide images using the carousel-control class as below.

How do you slide Bootstrap carousel?

Carousels require the use of an id (in this case id="myCarousel" ) for carousel controls to function properly. The class="carousel" specifies that this <div> contains a carousel. The . slide class adds a CSS transition and animation effect, which makes the items slide when showing a new item.


1 Answers

To avoid this delay, you could manually launch both carousels at the same time, and use customized treatments for events.

Option #1 :

  • Syncronized init
  • Simple launch events on both carousels
  • Pause on hover (I missed you didn't need it)
$('.carousel-sync').carousel('cycle');
$('.carousel-sync').on('click', '.carousel-control[data-slide]', function (ev) {
    ev.preventDefault();
    $('.carousel-sync').carousel($(this).data('slide'));
});
$('.carousel-sync').on('mouseover', function(ev) {
    ev.preventDefault();
    $('.carousel-sync').carousel('pause');
});
$('.carousel-sync').on('mouseleave', function(ev) {
    ev.preventDefault();
    $('.carousel-sync').carousel('cycle');
});
<div id="carousel-a" class="carousel slide carousel-sync">
  ...
</div>

<div id="carousel-b" class="carousel slide carousel-sync">
  ...
</div>

Bootply example

Option #2

  • Desynchronized init
  • Duplicate events on both carousels as soon as it occurs
  • No pause on hover
$('.carousel-sync').on('slide.bs.carousel', function(ev) {
    // get the direction, based on the event which occurs
    var dir = ev.direction == 'right' ? 'prev' : 'next';
    // get synchronized non-sliding carousels, and make'em sliding
    $('.carousel-sync').not('.sliding').addClass('sliding').carousel(dir);
});
$('.carousel-sync').on('slid.bs.carousel', function(ev) {
    // remove .sliding class, to allow the next move
    $('.carousel-sync').removeClass('sliding');
});
<div id="carousel-a" class="carousel slide carousel-sync" data-ride="carousel" data-pause="false">
  ...
</div>

<div id="carousel-b" class="carousel slide carousel-sync" data-ride="carousel" data-pause="false">
  ...
</div>

Please not the .sliding class is necessary, to avoid an infinite loop.

Bootply example

like image 186
zessx Avatar answered Sep 17 '22 13:09

zessx