Bootstrap Simple Thumbnail Gallery

Simple Bootstrap thumbnail gallery. good starting point

by Glynne Turner

HTML

<script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">


    
    <div class="col-sm-4 col-md-3 img-holder">
      <div class="img">
        <img class="gallery-image" src="https://thanet-web-design.com/CTE2/wp-content/uploads//2021/12/CanaryTrailEvents1.jpg" data-imgsrc="https://thanet-web-design.com/CTE2/wp-content/uploads//2021/12/CanaryTrailEvents1.jpg" alt=""/>
      </div>
    </div> 
    <div class="col-sm-4 col-md-3 img-holder">
      <div class="img">
        <img class="gallery-image" src="https://thanet-web-design.com/CTE2/wp-content/uploads/2021/11/NightRunner.jpg" data-imgsrc="https://thanet-web-design.com/CTE2/wp-content/uploads/2021/11/NightRunner.jpg" alt=""/>
      </div>
    </div> 
    <div class="col-sm-4 col-md-3 img-holder">
      <div class="img">
        <img  class="gallery-image" src="https://s3.envato.com/files/248344327/WY0A1256.jpg" data-imgsrc="https://s3.envato.com/files/248344327/WY0A1256.jpg" alt=""/>
      </div>
    </div> 
    <div class="col-sm-4 col-md-3 img-holder">
      <div class="img">
      </div>
    </div> 
    <div class=" col-sm-4 col-md-3 img-holder">
      <div class="img">
      </div>
    </div> 
    <div class="col-sm-4 col-md-3 img-holder">
      <div class="img">
      </div>
    </div> 
  </div>
  
</div>

JavaScript

$('.gallery-image').click(function(){
  var current = $(this).index('.gallery-image');
 	var $img_src= $(this).data('imgsrc');
  $('#active').attr('data-active', current)
  $('#gallery-img').attr('src', $img_src);
  $('#large-gallery').addClass('showgallery'); 
})

$('#prev , #next').click(function(){
	 var img_count = document.getElementsByClassName('gallery-image').length;
	 var current = $('#active').data('active');
   var imgN ='';
   if($(this).attr("id") == 'prev'){ 
		if(current == 0){ 
    	imgN = img_count -1;
     }else{ 
      imgN = current -1; 
     }
   }else{
      if (current == (img_count - 1) ){
      	imgN = 0;
      }else{
      	imgN = current +1;
      }
      
   } 
	 $('#active').data('active', imgN )
   var newImg = $('.gallery-image').eq(imgN).attr('data-imgsrc');
   $('#gallery-img').attr('src',newImg);

})

$('#close').click(function(){
$('#large-gallery').removeClass('showgallery'); 
})