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>
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:
Then the following JavaScript should control starting and stopping on any frame:
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:
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:
Instead, make sure you turn this off when you initialize the carousel like this:
This was my approach. You can style the .pause and .play class however you see fit, but I'm just using plain text for demo purposes:
Here is your detonator
For those who would like the play/pause buttons to switch off. jsFiddle example
Javascript:
HTML: