带有一个项目的Bootstrap轮播

时间:2018-02-06 09:36:08

标签: javascript jquery html css twitter-bootstrap

是否可以连续循环播放一个项目,但不断更改背景图像,而不必使用不同的背景图像多次重复代码。如果你查看我的代码,你可以看到我想要实现的目标。有一个更好的方法吗?或者我正在做最好的方式?



var $myCarousel = $('#slider');
$myCarousel.carousel();

function doAnimations(elems) {
  var animEndEv = 'webkitAnimationEnd animationend';
  
  elems.each(function () {
    var $this = $(this),
        $animationType = $this.data('animation');

    // Add animate.css classes to
    // the elements to be animated 
    // Remove animate.css classes
    // once the animation event has ended
    $this.addClass($animationType).one(animEndEv, function () {
      $this.removeClass($animationType);
    });
  });
}

// Select the elements to be animated
// in the first slide on page load
var $firstAnimatingElems = $myCarousel.find('.carousel-item:first')
                           .find('[data-animation ^= "animated"]');

// Apply the animation using the doAnimations()function
doAnimations($firstAnimatingElems);

// Attach the doAnimations() function to the
// carousel's slide.bs.carousel event 
$myCarousel.on('slide.bs.carousel', function (e) { 
  // Select the elements to be animated inside the active slide 
  var $animatingElems = $(e.relatedTarget)
                        .find("[data-animation ^= 'animated']");
  doAnimations($animatingElems);
});

#slider{
   height: 400px;
}

.carousel-caption h3:first-child{
  animation-delay: 1s;
}

.carousel-caption h3:nth-child(2){
  animation-delay: 2s;
}

.carousel-caption button{
  animation-delay: 3s;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet"/>
<div id="slider" class="carousel slide"> 
   <!-- indicators -->
    <ol class="carousel-indicators">
        <li data-target="#stairworld_slider" data-slide="0" class="active"></li> 
        <li data-target="#stairworld_slider" data-slide="1"></li>
    </ol> 
   
    <!-- sliders wrapper -->
    <div class="carousel-inner" role="listbox" style="height: 100%;">
        <div class="item active" style="background-color: blue; height: 100%;">
            <div class="carousel-caption">
                 <h3 data-animation="animated bounceInLeft">10% OFF</h3>
                 <h3 data-animation="animated bounceInLeft">ALL ORDERS</h3>
                 <button data-animation="animated bounceInLeft"><a href="/staircase-builder/">this is a button</a>
                </button>
            </div>
        </div>

        <div class="item" style="background-color: red; height: 100%;">
            <div class="carousel-caption">
                 <h3 data-animation="animated bounceInLeft">10% OFF</h3>
                 <h3 data-animation="animated bounceInLeft">ALL ORDERS</h3>
                 <button data-animation="animated bounceInLeft"><a href="/staircase-builder/">this is a button</a>
                </button>
            </div>
        </div>
    </div> 

    <!-- controlls -->
    <a class="left carousel-control" href="#stairworld_slider" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    </a>

    <a class="right carousel-control" href="#stairworld_slider" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    </a>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

请查看以下代码,了解它是否适​​合您。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
</head>
<body>

<div class="container">
  <h2>Carousel Example</h2>  
  <div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="2000">
    <!-- 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>-->
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
        <img src="https://www.w3schools.com/bootstrap/la.jpg" alt="Los Angeles" style="width:100%;">
      </div>

      <div class="item">
        <img src="https://www.w3schools.com/bootstrap/la.jpg" alt="Los Angeles" style="width:100%;">
      </div>   
    </div>

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

</body>
</html>