是否可以防止Bootstrap轮播仅在鼠标悬停时播放?

时间:2019-02-26 13:15:28

标签: carousel

是否可以使Bootstrap 4 Carousel仅在悬停时播放?像通常一样,它是静止图像,但是在鼠标悬停时,它开始循环旋转木马,在鼠标悬停时,它暂停。

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');
});

1 个答案:

答案 0 :(得分:1)

首先通过将data-pause设置为false来禁用轮播的默认行为,使其在悬停时停止

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

使用以下js管理悬停行为

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

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

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

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