Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Play/Pause button to bootstrap carousel

I have read almost all the threads regarding to the bootstrap carousel, but haven't found an answer to my question. I have added two control buttons (play/pause) to the carousel, but each button fires the function from the default/first slide. I would like to be able to pause the current slide and play from the current slide whenever the click event happens.

Here is my code:

<script src="/_catalogs/masterpage/UHN/js/bootstrap.min.js"></script>
<script>
    $(function () {
        $('.carousel').carousel({ interval:6000 });  

        $('#playButton').click(function () {
            $('#homeCarousel').carousel('cycle');
        });
        $('#pauseButton').click(function () {
            $('#homeCarousel').carousel('pause');
        });
    });

</script>

And the two controls:

<!--Carousel controls -->
<button id="playButton">Play</button>
<button id="pauseButton">Pause</button>
like image 493
Mojgan Avatar asked Dec 16 '13 15:12

Mojgan


People also ask

How do I pause bootstrap carousel?

You'll need to call the pause function like this: $("#myCarousel"). carousel('pause'); . This is how all methods are called on bootstrap components, following the jQuery UI convention. You can restart the carousel by listening to the blur event then calling the cycle method.

How do I stop bootstrap carousel autoplay?

To turn off the autoplay set data-interval="false" to the carousel element.

How do I add a button to carousel?

You can do so by setting the carousel's position to relative and then setting then placing your button within the you want the button to appear in. After that is done, you should set the buttons position to absolute.

How do I add a play pause button in HTML?

HTML Audio/Video DOM pause() Method The pause() method halts (pauses) the currently playing audio or video. Tip: Use the play() method to start playing the current audio/video.


1 Answers

This should be working. I'd make sure that your click events are firing as you expect them to because some elements of the bootstrap carousel can appear above your elements and prevent clicks.

If you add the following controls in HTML:

<div id="carouselButtons">
    <button id="playButton" type="button" class="btn btn-default btn-xs">
        <span class="glyphicon glyphicon-play"></span>
     </button>
    <button id="pauseButton" type="button" class="btn btn-default btn-xs">
        <span class="glyphicon glyphicon-pause"></span>
    </button>
</div>

Then the following JavaScript should control starting and stopping on any frame:

$('#playButton').click(function () {
    $('#homeCarousel').carousel('cycle');
});
$('#pauseButton').click(function () {
    $('#homeCarousel').carousel('pause');
});

As related to my first point, to get them to appear properly within the carousel, you can style the button group with the following CSS:

#carouselButtons {
    margin-left: 100px;
    position: absolute;
    bottom: 0px;
}

Working Demo in jsFiddle

For this functionality to make the most sense, you probably want the carousel to continue moving, even when hovering over it (otherwise after the play selection is made, the cycling behavior won't start up again until you move the mouse).

According to the carousel docs:

Option - pause
Default - "hover" - Pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on mouseleave.

Instead, make sure you turn this off when you initialize the carousel like this:

$('#homeCarousel').carousel({
    interval:2000,
    pause: "false"
});
like image 57
KyleMit Avatar answered Sep 18 '22 11:09

KyleMit