Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link two Twitter Bootstrap carousels together using one control

I'm just learning javascript so please bear with me here. I'm trying to initiate two carousels in bootstrap using the same control.

The way it's setup right now, href tags containing the carousel's ID is controlling each carousel. Anyone have advice on how to link both together using the same control with minimal modifications?

Here's the HTML:

<div id="asHero">
                <div id="walkThrough" class="carousel slide">
                    <!-- Carousel items -->
                    <div class="carousel-inner">
                        <div class="active item">
                            <div class="span5">
                                <h1>Discover</h1>
                                <p class="heroDesc">Explore the active side of the world around you.</p>
                            </div>
                            <div class="span6">
                                <img src="img/landing/icontest.png" />
                            </div>
                        </div>
                        <div class="item">
                            <div class="span5">
                                <h1>Track</h1>
                                <p class="heroDesc">Keep tabs on frequency and personal bests.</p>
                            </div>
                            <div class="span6">
                                <img src="img/landing/icontest2.png" />
                            </div>
                        </div>
                        <div class="item">
                            <div class="span5">
                                <h1>Conquer</h1>
                                <p class="heroDesc">Earn points and awards for everything you do in your active life.</p>
                            </div>
                            <div class="span6">
                                <img src="img/landing/icontest3.png" />
                            </div>
                        </div>
                    </div>
                    <!-- Carousel nav -->

                    <a class="carousel-control right visible-desktop" href="#walkThrough" data-slide="next">&rsaquo;</a>
                </div>
            </div> <!-- /asHero -->
            <div id="asPhone">
                <div id="walkThrough-2" class="carousel slide">
                    <!-- Carousel items -->
                    <div class="carousel-inner">
                        <div class="active item">
                            <div class="span5">
                                <h1>Discover</h1>
                                <p class="heroDesc">Explore the active side of the world around you.</p>
                            </div>
                            <div class="span6">
                                <img src="img/landing/icontest.png" />
                            </div>
                        </div>
                        <div class="item">
                            <div class="span5">
                                <h1>Track</h1>
                                <p class="heroDesc">Keep tabs on frequency and personal bests.</p>
                            </div>
                            <div class="span6">
                                <img src="img/landing/icontest2.png" />
                            </div>
                        </div>
                        <div class="item">
                            <div class="span5">
                                <h1>Conquer</h1>
                                <p class="heroDesc">Earn points and awards for everything you do in your active life.</p>
                            </div>
                            <div class="span6">
                                <img src="img/landing/icontest3.png" />
                            </div>
                        </div>
                    </div>
                    <!-- Carousel nav -->


                </div>
            </div> <!-- /asPhone -->

Here's a link to the Javascript: Twitter Bootstrap Carousel JS

Any help would be greatly appreciated!

like image 944
SGLVEGAS Avatar asked Oct 15 '12 19:10

SGLVEGAS


People also ask

How do I put multiple carousels 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.

What is the purpose of the Corousel in bootstrap?

The carousel is a slideshow for cycling through a series of content, built with CSS 3D transforms and a bit of JavaScript. It works with a series of images, text, or custom markup. It also includes support for previous/next controls and indicators.

What is data BS interval?

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-bs- , as in data-bs-interval="" . The amount of time to delay between automatically cycling an item.

How do I change the timing of a bootstrap carousel slide?

You can simply use the data-interval attribute of the carousel class. It's default value is set to data-interval="3000" i.e 3seconds. All you need to do is set it to your desired requirements.


2 Answers

You can access the carousels using their class carousel instead of their IDs. This way you can handle multiple carousels with one control.

Add an extra control button to your template

<a href="#" class="btn">Next</a>

And bind a click event to this button to control the carousels

$('.btn').on('click', function(e) {
  e.preventDefault()
  $('.carousel').carousel('next')
})

The same goes for the prev control. For more information have a look at the docs.

like image 52
zemirco Avatar answered Jan 02 '23 19:01

zemirco


This is a partial solution, unfortunately. The reason being I can see no way to figure out from the triggered event which direction the thing is going.

$('#walkThrough').carousel({
  interval: 5000 // Slide every 5 seconds
});

$('#walkThrough-2').carousel({
  interval: false // This one does not slide on its own
});

$('#walkThrough').bind('slide', function() {
  $('#walkThrough-2').carousel('next');
});
like image 41
Marcin Wyszynski Avatar answered Jan 02 '23 20:01

Marcin Wyszynski