Select Page

The first slide will be randomly chosen from available slides every time page reloads.

DEMO

Open demo in a full window – https://demos.webdevpuneet.com/bootstrap/v4/random-slide/index.html

Logic Used (jQuery)

Just need to add this code after including jQuery and Bootstrap 4 scripts. The logic is simply to get a random number out of the total number of slides used in the carousel. The index for that slide will be (number – 1) and then call that slide first by “$(‘.carousel’).carousel( randomNumIndex );” method.

The full source code can be found below.

$(document).ready(function(){
	var slideNum = $('.carousel-inner .carousel-item').length;
	var randomNum = Math.floor(Math.random() * slideNum) + 1;
	var randomNumIndex = randomNum - 1;
	$('.carousel').carousel( randomNumIndex );
	$('.carousel-item').removeClass('transparent');
});

Demo Source Code

This is the full source code of the demo. You can use it as a reference to check where to include the logic script.


<html>
  <head>
    <meta charset="UTF-8" />
    <title>Bootstrap 4 Carousel - Random Slide</title>
    <meta name="description" content="Bootstrap 4 Carousel - Random Slide" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    <style>
      body{
        padding: 20px;
      }
      .carousel{
        width:780px;
        max-width:100%;
      }
      .transparent{
        opacity: 0!important;
      }
    </style>
  </head>
  <body>
    <h1 style="margin-bottom:20px;">Bootstrap 4 Carousel - Random Slide</h1>

    <a href="">Click to refresh</a>
    <br/>
    <br/>
    <div id="carouselExampleIndicators" class="carousel slide carousel-fade" data-ride="carousel">
      <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="3"></li>
      </ol>
      <div class="carousel-inner">
        <div class="carousel-item active transparent">
          <img src="https://projects.webdevpuneet.com/demos/bootstrap/v4/random-slide/img/slide1.png" class="d-block w-100" alt="...">
        </div>
        <div class="carousel-item">
          <img src="https://projects.webdevpuneet.com/demos/bootstrap/v4/random-slide/img/slide2.png" class="d-block w-100" alt="...">
        </div>
        <div class="carousel-item">
          <img src="https://projects.webdevpuneet.com/demos/bootstrap/v4/random-slide/img/slide3.png" class="d-block w-100" alt="...">
        </div>
        <div class="carousel-item">
          <img src="https://projects.webdevpuneet.com/demos/bootstrap/v4/random-slide/img/slide4.png" class="d-block w-100" alt="...">
        </div>
      </div>
      <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>

    <script>
      $(document).ready(function(){
        var slideNum = $('.carousel-inner .carousel-item').length;
        var randomNum = Math.floor(Math.random() * slideNum) + 1;
        var randomNumIndex = randomNum - 1;
        $('.carousel').carousel( randomNumIndex );
        $('.carousel-item').removeClass('transparent');
      });
    </script>

  </body>
</html>

Download files