Add Play/Pause button to bootstrap carousel

后端 未结 3 1874
孤城傲影
孤城傲影 2020-12-14 03:58

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 car

3条回答
  •  温柔的废话
    2020-12-14 04:10

    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:

    $('#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"
    });
    

提交回复
热议问题