Simple Bootstrap Gallery

Simple bootstrap with click and arrow key function

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 id="large-gallery">
  <div class="container gallery-inner"> 
      <img src="" id="gallery-img" /> 
      <div id="active"></div>
  </div>
  <span id="prev"><i class="fa fa-chevron-circle-left"></i>
</span>
  <span id="next"><i class="fa fa-chevron-circle-right"></i>
</span>
  <span id="close"><i class="fa fa-times-circle"></i></span> 
</div>
<div class="container">
  <div class="row">
    <!-- FOREACH STARTS HERE -->
    <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/Tree.jpg" alt=""  data-imgsrc="https://thanet-web-design.com/CTE2/wp-content/uploads/2021/11/Tree.jpg" />
      </div>
    </div> 
    <!-- FOREACH ENDS HERE -->
    <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"...

CSS

*{box-sizing:border-box}
.img-holder{height:200px;padding:5px;}
.img{ border: 1px solid blue; width:100%; height:100%; overflow-x:hidden;overflow-y:hidden}
.img img{min-height:100%;position:relative; width:100%; height:auto  }
#large-gallery{display:none; width:100vw; height:100vh;  z-index:999999; position:fixed }
.showgallery{display:flex !important;  background-color:rgba(0,0,0,.9); height:100vh;flex-direction: column;justify-content: center; }
.gallery-inner{width:100%; text-align:center}
#gallery-img{max-height:70vh; max-width:80vw}

#prev, #next {font-size:60px; position:absolute; top:calc (50vh - 30px);  color:#ffffff; transition: all .3s}
#close{font-size:60px; position:absolute; top:10px; right:10px;  color:#ffffff; transition: all .3s}
#prev:hover, #next:hover,#close:hover{cursor:pointer; opacity:.7}
#prev {left:calc(7vw - 30px)}
#next{right:calc(7vw - 30px)}



.img-holder{}

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);

})
$(document).ready().bind("keyup", function (event) {
 
    var ekeyCode = event.keyCode; 
 
	 var img_count = document.getElementsByClassName('gallery-image').length;
	 var current = $('#active').data('active');
   var imgN ='';
   if (ekeyCode === 37){ 
		if(current == 0){ 
    	imgN = img_count -1;
     }else{ 
      imgN = current -1; 
     }
   }else{
	   if (ekeyCode === 39){
      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);
	

  	$(document).bind('keydown');
});

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