Bootstrap carousel animate top to bottom

      No Comments on Bootstrap carousel animate top to bottom

Bootstrap carousel animate top to bottom instead of left to right:

See the Pen animating top to bottom-bootstrap carousel by scanfcode (@scanfcode) on CodePen.


For creating a Bootstrap carousel, you need to include bootstrap in your website. Bootstrap can either be downloaded from getbootstrap.com or you can use bootstrap CDN. The code is:

 <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

For creating a simple carousel which animates from left to right bootstrap provides code snippet that is:

 <div id="myCarousel" class="carousel slide" data-ride="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>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
        <img src="scanf.jpg" alt="Bootstrap carousel animate top to bottom">
      </div>

      <div class="item">
        <img src="scanf.jpg" alt="Bootstrap carousel animate top to bottom">
      </div>
    
      <div class="item">
        <img src="scanf.jpg" alt="Bootstrap carousel animate top to bottom">
      </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>

Bootstrap carousel animate top to bottom – the trick

To animate carousel form top to bottom , we add an additional class top-to-bottom

.top-to-bottom .carousel-inner {
  height: 100%;
}

//setting animation properties
.carousel.top-to-bottom .item {
   -webkit-transition: 0.6s ease-in-out top;
   -moz-transition: 0.6s ease-in-out top;
   -ms-transition: 0.6s ease-in-out top;
   -o-transition: 0.6s ease-in-out top;
   transition: 0.6s ease-in-out top;
}

//first item (active) is set to  0 from top
.carousel.top-to-bottom .active {
   top: 0;
}

//next item set to 400 from top
.carousel.top-to-bottom .next {
   top: 400px;
}

//previous item set to -400 from top
.carousel.top-to-bottom .prev {
   top: -400px;
}
.carousel.top-to-bottom .item {
   left: 0;
}

The main function is done by the css transition applied in the new class.

You can also refer to
Bootstrap text carousels

Leave a Reply

Your email address will not be published. Required fields are marked *