单击右/左控件后如何防止Bootstrap轮播恢复幻灯片动画

Posted

技术标签:

【中文标题】单击右/左控件后如何防止Bootstrap轮播恢复幻灯片动画【英文标题】:How to prevent Bootstrap carousel from resuming of the slide animation after clicking on right/left controls 【发布时间】:2016-12-03 09:26:42 【问题描述】:

我正在使用 Bootstrap Carousel 来展示一些图像。 我已经从轮播 div 中删除了 slide 类和 data-ride="carousel",以便删除自动滑动动画。

但是,当您单击右/左控件之一来更改图像时,幻灯片动画会继续

.carousel-inner > .item > img,
.carousel-inner > .item > a > img 
  width: 70%;
  margin: auto;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<div class="container">
  <br>
  <div id="myCarousel" class="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img src="http://www.w3schools.com/bootstrap/img_chania.jpg"   >
      </div>

      <div class="item">
        <img src="http://www.w3schools.com/bootstrap/img_chania2.jpg"   >
      </div>

      <div class="item">
        <img src="http://www.w3schools.com/bootstrap/img_flower.jpg"   >
      </div>

      <div class="item">
        <img src="http://www.w3schools.com/bootstrap/img_chania2.jpg"   >
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

我也尝试在上面添加一个 jQuery 行

$("#myCarousel").carousel("pause");

但这似乎没有任何帮助。

我能做什么?

谢谢

【问题讨论】:

【参考方案1】:

尝试使用此 javascript 行,而不是您在答案末尾写的那行:

$(document).ready(function() 
 $("#myCarousel").carousel(interval: 500, pause: "hover");

您可以将间隔调整为您想要的任何值,并且即使在使用左/右控件之后,只要鼠标悬停在轮播上,它就会暂停,希望这会有所帮助!

【讨论】:

【参考方案2】:

尝试添加这个脚本:

$('#myCarousel').on('slid.bs.carousel', function () 
  $(this).carousel('pause');
)
slid.bs.carousel 事件“在轮播 has completed its slide transition 时触发”。 .carousel('pause') 方法“stops the carousel 循环浏览项目”。

请检查结果:

$('#myCarousel').on('slid.bs.carousel', function () 
  $(this).carousel('pause');
)
.carousel-inner > .item > img,
.carousel-inner > .item > a > img 
  width: 70%;
  margin: auto;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<div class="container">
  <br>
  <div id="myCarousel" class="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img src="http://www.w3schools.com/bootstrap/img_chania.jpg"   >
      </div>

      <div class="item">
        <img src="http://www.w3schools.com/bootstrap/img_chania2.jpg"   >
      </div>

      <div class="item">
        <img src="http://www.w3schools.com/bootstrap/img_flower.jpg"   >
      </div>

      <div class="item">
        <img src="http://www.w3schools.com/bootstrap/img_chania2.jpg"   >
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

【讨论】:

以上是关于单击右/左控件后如何防止Bootstrap轮播恢复幻灯片动画的主要内容,如果未能解决你的问题,请参考以下文章

Twitter-Bootstrap 5 轮播指示器和控件不起作用。为啥?

如何防止鼠标右键单击onMouseDown()反应js?

页面加载后,如何使 Bootstrap js 轮播自动循环?

如何防止 Bootstrap 轮播图像堆叠? HTML/CSS(使用 PHP 显示图像)

如何删除留下的空白和轮播权

如何将 Cloudinary 照片集成到带有控件的 Bootstrap 轮播?