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>
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.
To turn off the autoplay set data-interval="false" to the carousel element.
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.
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.
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;
}
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"
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With