Shibin Mariyan Stanly
Shibin Mariyan Stanly

Reputation: 331

Is it possible to prevent the Bootstrap carousel play only on mouse hover?

is it possible to make Bootstrap 4 Carousel to play ONLY on hover? Like normally it's a still image, but on mousehover it starts to cycle the carousel and on mouseout it pauses.

slider.html

<div id="carousel-example-generic" class="carousel slide carousel-fade slider slideInUp"
                data-ride="carousel">
                <div class="row">
                    <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
                        <div class="tab-section">
                            <div class="user">
                                <ul class=" carousel-indicators">
                                    <li data-target="#carousel-example-generic" data-slide-to="0"
                                        class="active wow slideInUp " data-wow-delay=".1s">
                                        <a href="" class="transition-fast">
                                            <span class="count">01</span>
                                            <span class="text">
                                                Lorem Lopsun
                                            </span>
                                        </a>
                                    </li>
                                    <li data-target="#carousel-example-generic" data-slide-to="1" class=" wow slideInUp"
                                        data-wow-delay=".2s">
                                        <a href="" class="transition-fast">
                                            <span class="count">02</span>
                                            <span class="text">
                                               Lorem Lopsun
                                            </span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-5 col-md-6 wow slideInRight" data-wow-delay="1s">
                        <div class="carousel-inner wow slideInUp" data-wow-delay="2s" role="listbox">
                            <div class="item active">
                                <img src="assets/img/first-01.png" alt="">
                            </div>
                            <div class="item">
                                <img src="assets/img/first-02.png" alt="">
                            </div>

                        </div>
                    </div>
                </div>
            </div>

script.js

     $('#carousel-example-generic').hover(function(){
   $("#carousel-example-generic").carousel('cycle');
},function(){
   $("#carousel-example-generic").carousel('pause');
});

Upvotes: 0

Views: 588

Answers (1)

Sreekuttan.M Achari
Sreekuttan.M Achari

Reputation: 26

First disable the default behaviour of the carousel to stop on hover by putting setting the data-pause to false

id="carousel-example-generic" class="carousel slide carousel-fade slider slideInUp" data-pause="false"

Use the following js to manage the hover behaviour

  $('.carousel').carousel({
    interval: 2000
  })

  $('.carousel').carousel('pause') ;

  $('.carousel').hover(function(){
    $('.carousel').carousel('cycle') ;
  }) ;

  $('.carousel').mouseleave(function(){
    $('.carousel').carousel('pause') ;
  }); 

Upvotes: 1

Related Questions