Javascript/CSS Lightbox only working for first element?
Asked Answered
I

3

8

I am trying to open each of my photos in a Javascript/CSS lightbox upon each of the photos being clicked.

At the moment, only the first photo in my list of photos is opening up in the lightbox. The other photos do not open in the lightbox.

Please can someone explain/show me why this is and explain/show me the correct way of doing this?

Here is my HTML/PHP:

<?php $result4 = $mysqli->query("SELECT * FROM user_data WHERE user_id = $p_id");
    if($result4->num_rows > 0) {
    echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
    echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
    echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
    echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
    echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';
    echo '<div class="profile_gallery_image"><img id="myImg" src='.$image.' height="100%" width="100%" /></div>';

    } ?>  

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
</div>

CSS:

<style>

#myImg {
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {
    transform: scale(1.09);
    border: 1px solid #a5a5a5;

}

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 10; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (image) */
.modal-content {
  margin: auto;
  display: block;
  width: 100%;
  max-width: 500px;
  border: 1px solid #f1f1f1;
  border-radius: 0px;

}

/* Caption of Modal Image */

/* Add Animation */
.modal-content, #caption {  
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {-webkit-transform:scale(0)} 
  to {-webkit-transform:scale(1)}
}

@keyframes zoom {
  from {transform:scale(0)} 
  to {transform:scale(1)}
}

/* The Close Button */
.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
</style>

Javascript:

   <script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
img.onclick = function(){
  modal.style.display = "block";
  modalImg.src = this.src;

}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
var span2 = document.getElementsByClassName("modal")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}
span2.onclick = function() { 
  modal.style.display = "none";
}
</script>

Thanks in advance.

Iorgos answered 8/3, 2019 at 22:50 Comment(7)
You have duplicate id's (id="myImg"). All id's on a page must be unique; this is a strict rule.Ferneferneau
I don't see any jQuery... Only JS. Why tagging jQuery?Bullnecked
@MarsAndBack could I update my code in a way to work of a class instead of an ID? and if so, how could i do this? thanksIorgos
@LouysPatriceBessette sorry my mistake, i'm still learning and a bit unsure of the difference between Jquery/Javascript. Question amended. thANKSIorgos
@MikeDawson javascript is the language, jQuery is a library written in javascript to make some tasks easier.Tacheometer
@MikeDawson the equivalent to getElementById for classes is getElementsByClassName, keep in mind this returns an array of items here is some more documentation on it. developer.mozilla.org/en-US/docs/Web/API/Document/…Tacheometer
@MikeDawson Some of us can help you better if you had a runnable code sample in your post. See if you can extract your HTML/JS/CSS and paste it into the post as Code Snippet.Ferneferneau
A
0

The good news is that you need neither an ids nor class on your images.

  • Listen for a click on the container around the images, not the images themselves. This technique is called event delegation and is better for performance. Rather than binding n event listeners for every image you may have, we add only one listener on the container of the images
  • When clicked, get the src and alt of the target element and pass it to the showModal function. Fill the modal image's src and alt with the passed values

Advice for later

It seems you were/are planning on showing a caption in your modal which is based on the passed-in alt value. Consider using figcaption in the modal.

The HTML or Figure Caption element represents a caption or legend describing the rest of the contents of its parent element.

Here's an example to get you started. I'll leave the styling of the modal/modal content to you. I used a figure element inside the modal for semantic value.

Demo

var modal = document.getElementById('myModal');

var imgContainer = document.querySelector('.imageContainer');
var modalContentImage = modal.querySelector('.modal-content-image');

imgContainer.addEventListener('click', function(e) {
  if (e.target.nodeName === "IMG") {
    showModal({src: e.target.src, alt: e.target.alt})
  }
});

function showModal(opts) {
  modal.style.display = "block";
  modalContentImage.setAttribute('src', opts.src);
  modalContentImage.setAttribute('alt', opts.alt);
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
var span2 = document.getElementsByClassName("modal")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}
span2.onclick = function() {
  modal.style.display = "none";
}
#myImg {
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {
  transform: scale(1.09);
  border: 1px solid #a5a5a5;
}

/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 10;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.9);
  /* Black w/ opacity */
}

/* Modal Content (image) */

.modal-content-image {
  margin: auto;
  display: block;
  width: 100%;
  max-width: 500px;
  border: 1px solid #f1f1f1;
  border-radius: 0px;
}

/* Caption of Modal Image */

/* Add Animation */

.modal-content,
#caption {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    -webkit-transform: scale(0)
  }
  to {
    -webkit-transform: scale(1)
  }
}

@keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}

/* The Close Button */

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

/* 100% Image Width on Smaller Screens */

@media only screen and (max-width: 700px) {
  .modal-content {
    width: 100%;
  }
}

/* Presentation only */

.imageContainer {
  min-height: 100vh;
  display: flex;
  align-items: flex-end;
  overflow-x: auto;
}

.imageContainer img {
  width: 100px;
  flex-shrink: 0;
}

html,
body {
  margin: 0;
  padding: 0;
}
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <figure class="modal-content">
    <img class="modal-content-image" alt>
  </figure>
</div>

<div class="imageContainer">
  <img src="https://picsum.photos/300/300/?image=0" alt="CPU 1">
  <img src="https://picsum.photos/300/300/?image=1" alt="CPU 2">
  <img src="https://picsum.photos/300/300/?image=2" alt="CPU 3">
  <img src="https://picsum.photos/300/300/?image=3" alt="CPU 4">
  <img src="https://picsum.photos/300/300/?image=4" alt="CPU 5">
  <img src="https://picsum.photos/300/300/?image=5" alt="CPU 6">
  <img src="https://picsum.photos/300/300/?image=6" alt="CPU 7">
  <img src="https://picsum.photos/300/300/?image=7" alt="CPU 8">
  <img src="https://picsum.photos/300/300/?image=8" alt="CPU 9">
  <img src="https://picsum.photos/300/300/?image=9" alt="CPU 10">
  <img src="https://picsum.photos/300/300/?image=10" alt="CPU 11">
  <img src="https://picsum.photos/300/300/?image=11" alt="CPU 12">  
</div>

jsFiddle

Atlantis answered 8/3, 2019 at 23:34 Comment(0)
W
0

Your code contains multiple id, instead try to use class and get your image class by document.getElementsByClassName then bind the click event on each image inside loop, try to add your php code to this revised code below

// Get the modal
var modal = document.getElementById('myModal');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var imgs = document.getElementsByClassName("myImg");
var modalImg = document.getElementById("img01");

for(var i = 0; i < imgs.length; i++)
{
  imgs[i].onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;

  }
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
var span2 = document.getElementsByClassName("modal")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}
span2.onclick = function() { 
  modal.style.display = "none";
}
#myImg {
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {
    transform: scale(1.09);
    border: 1px solid #a5a5a5;

}

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 10; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (image) */
.modal-content {
  margin: auto;
  display: block;
  width: 100%;
  max-width: 500px;
  border: 1px solid #f1f1f1;
  border-radius: 0px;

}

/* Caption of Modal Image */

/* Add Animation */
.modal-content, #caption {  
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {-webkit-transform:scale(0)} 
  to {-webkit-transform:scale(1)}
}

@keyframes zoom {
  from {transform:scale(0)} 
  to {transform:scale(1)}
}

/* The Close Button */
.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
    <div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
    <div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
    <div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
    <div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
    <div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>
    <div class="profile_gallery_image"><img class="myImg" src='https://via.placeholder.com/150' height="100%" width="100%" /></div>



<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
</div>
Wanton answered 8/3, 2019 at 23:34 Comment(0)
Y
0

Pure Javascript Lightbox / Image Popup Modal

I wish to share a Pure Javascript Lightbox or Image Popup Modal which is completely compliant with the Navigation guidelines of ARIA and is lightweight and easy to implement because :

  1. It does not require any JQUERY hence an additional import request to CDN and the buffering of entire JQUERY library can be prevented.
  2. This is a deploy and forget kind of design where we only need to add the LIGHTBOX related HTML, CSS and JAVASCRIPT without any need to add unique IDs or CLASSes to the HTML tags of images or its parent A tag. That is why, later addition or removal of images from your exisitng webpages will not require any change in the Javascript or the ID/CLASS attributes of IMG/A tags.

  1. To implement this, a precursor is that all the images in IMG tag shall be the only Child node of a parent A tag like <a title="" href=""><img alt="" src=""/></a>.

    • Also, One thing to keep in mind is that there should be no White spaces in between opening and closing of A tag and IMG tag and it should appear as <a><img/></a>.

    • Since only IMG tag images are loaded when page is downloaded while A tags are not that is why creating a parent A tag for all the IMG tag allows to store smaller size of the images in IMG tag and larger size of the same images in the parent A tag.

    • Combining IMG tags with Loading="lazy" will help further speed up the page loading.

  2. This Lightbox is simple in working when implemented on a web page i.e. you Click, Touch or press Enter key on any image of IMG tag on page and a Modal or Lightbox will Popup to display the same URL image or a different one stored in the parent A tag of the IMG tag.

    • Same or Different URL for storing different quality images depend upon your choice. Former is easier to implement while later has a better performance.

Now let us see some code :


This a Sample HTML of A tags containing Images and URL to other pages.

<a href="https://1.bp.blogspot.com/-jNZtfo_qgNM/YHH_XF6FooI/AAAAAAAAC5E/y_tNUslqFPITSVncCkF3zyEC-RROSvETgCLcBGAsYHQ/s328/1.jpg" title="first image"><img alt="first image" src="https://1.bp.blogspot.com/-jNZtfo_qgNM/YHH_XF6FooI/AAAAAAAAC5E/y_tNUslqFPITSVncCkF3zyEC-RROSvETgCLcBGAsYHQ/s320/1.jpg"/></a>

<a href="https://1.bp.blogspot.com/-F0sshQGKu8Y/YHH_XL41aDI/AAAAAAAAC5M/fyAeo4X2tFw-RN-g8YFxNcel0WivQjj5gCLcBGAsYHQ/s400/2.jpg" title="second image"><img alt="second image" src="https://1.bp.blogspot.com/-F0sshQGKu8Y/YHH_XL41aDI/AAAAAAAAC5M/fyAeo4X2tFw-RN-g8YFxNcel0WivQjj5gCLcBGAsYHQ/s320/2.jpg"/></a>

<a href="https://1.bp.blogspot.com/-xk_pNZ1fh7o/YHH_XEROwmI/AAAAAAAAC5I/-WOsyfKgtSMRzXQBeEX-yjRX8TBJuEkFwCLcBGAsYHQ/s400/3.jpg" title="third image"><img alt="third image" src="https://1.bp.blogspot.com/-xk_pNZ1fh7o/YHH_XEROwmI/AAAAAAAAC5I/-WOsyfKgtSMRzXQBeEX-yjRX8TBJuEkFwCLcBGAsYHQ/s320/3.jpg"/></a>

<a href="https://1.bp.blogspot.com/-e3gjnYR7exE/YHH_YHRQgLI/AAAAAAAAC5Q/kgQYFsvBjuYPAXTjzMFkzvsRT6JgQlkywCLcBGAsYHQ/s720/4.jpg" title="fourth image"><img alt="fourth image" src="https://1.bp.blogspot.com/-e3gjnYR7exE/YHH_YHRQgLI/AAAAAAAAC5Q/kgQYFsvBjuYPAXTjzMFkzvsRT6JgQlkywCLcBGAsYHQ/s320/4.jpg"/></a>

<a href="https://1.bp.blogspot.com/-zGPorKCAHqw/YHH_YHcIpSI/AAAAAAAAC5U/Jx2serYqk58fa_HSf1GPwDZu2nT1c8kJgCLcBGAsYHQ/s1280/5.jpg" title="fifth image"><img alt="fifth image" src="https://1.bp.blogspot.com/-zGPorKCAHqw/YHH_YHcIpSI/AAAAAAAAC5U/Jx2serYqk58fa_HSf1GPwDZu2nT1c8kJgCLcBGAsYHQ/s320/5.jpg"/></a>

<a href="https://anubhavyadavjovian.blogspot.com/">Anubhav yadav</a>

Let say we have five images on our webpage and all are in the format <a title="" href=""><img alt="" src=""/></a> as defined above.

To show that this Lightbox is dynamic, we have included an additional <a href="https://anubhavyadavjovian.blogspot.com/">Anubhav yadav</a>, which will behave like a normal A Href Tag when Clicked, Touched or Enter key is pressed on it, while the A tags with only IMG tag as a Child will popup the Lightbox.


<div id='lightbox-home'>
  <div id='lightbox-container' onclick='hideoverlay()'>
    <img alt='' id='lightbox-cont-img' onclick='hideoverlay()' src=''/>
  </div>
</div>

The Real HTML code above is for our Lightbox with a "lightbox-container" ID appearing as a translucent black background with an image displaying tag with "lightbox-cont-img" ID.


#lightbox-container {
  z-index:2000;
  position:fixed;
  bottom:-5000px;
  left:0px;
  width:100%;
  height:100%;
  margin:0px;
  background-color: rgba(38, 38, 38, 0.85);
  transition: all 0.4s ease-out;

  display:flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;
}
#lightbox-container.showcontainer {
  bottom:0px;
}
#lightbox-container img {
  max-width:95%;
  max-height:95%;
  object-fit:contain;
}
:focus {
  border: 2px solid gold;
}

Above is the Real CSS for the decoration of Lightbox as well as creating transitions while appearing and disappearing.


// Select all A tags with IMG child nodes
var atagswithimgtag = document.querySelectorAll("a[href]");

// then prevent the default behaviour of A tags by preventing of opening new page by HREF
// as well as collect all the HREF of A tags with images to enable RIGHT and LEFT arrow key
var allimgurlarray = [];
for(i=0;i<atagswithimgtag.length;i++){
  var childAIMGtag = atagswithimgtag[i].childNodes;
  if (childAIMGtag[0].nodeType != Node.TEXT_NODE) // or if (el[i].nodeType != 3)
  {
    // this seems too be a A tag with IMG tag as Childnode

    // first we need to prevent the default behaviour of opening the IMG in New Tab
    atagswithimgtag[i].addEventListener("click", function(event){
      event.preventDefault();
    });

    // second is when we need to fill image URL aray with A HREF
    var listofnodes = atagswithimgtag[i];
    allimgurlarray[i] = [];
    allimgurlarray[i][0] = i;
    allimgurlarray[i][1] = " Image URL is ";//listofnodes.getAttributeNode("title").value;
    allimgurlarray[i][2] = listofnodes.getAttributeNode("href").value;
  }
  console.log(childAIMGtag[0].innerHTML);
}

// now we have to deal with Keyboard events
document.onkeydown = function(event){
  if(event.keyCode==27){ // If ESC key is pressed
    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){ // LIGHTBOX ON
      document.getElementById("lightbox-container").classList.remove("showcontainer");
    }
  }
  else if(event.keyCode==13) { // ENTER key pressed
    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){ // LIGHTBOX ON
      document.getElementById("lightbox-container").classList.remove("showcontainer");
    }
    else { // LIGHTBOX OFF
      var currentEventTarget = document.activeElement;
      if(currentEventTarget.tagName=='A'){
        var entertargetchild = currentEventTarget.childNodes;
        if(entertargetchild[0].tagName=='IMG'){
          var hrefofparent = currentEventTarget.getAttribute("href");
          document.getElementById("lightbox-cont-img").setAttribute("src", hrefofparent);

          document.getElementById("lightbox-container").classList.add("showcontainer");
          document.getElementById("lightbox-cont-img").focus();
        }
      }

    }
  }
  else if(event.keyCode==9) { // TAB key pressed
    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){ // LIGHTBOX ON
      document.getElementById("lightbox-container").classList.remove("showcontainer");
    }
  }
  else if(event.keyCode==37) { // Left arrow key
    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){ // LIGHTBOX ON
      // first get the URL of image displayed in the LIGHT BOX
      var currimgsrc = document.getElementById("lightbox-cont-img").getAttribute("src");

      // now match the sequence number in the array 
      var serialofarray = 0;
      for(k=0;k<allimgurlarray.length;k++){
        if(currimgsrc == allimgurlarray[k][2]){
          serialofarray = allimgurlarray[k][0];
        }
      }

      // with LEFT arrow, we are supposed to reduce the sequence and then use its ATTR SRC to LIGHT BOX
      if(serialofarray<=0){
        serialofarray = allimgurlarray.length - 1;
      }
      else {
        serialofarray = serialofarray - 1;
      }
      console.log("Left Arrow : "+serialofarray);
      document.getElementById("lightbox-cont-img").setAttribute("src", allimgurlarray[serialofarray][2]);

    }
  }
  else if(event.keyCode==39) { // RIGHT Arrow
    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){
      // first get the URL of image displayed in the LIGHT BOX
      var currimgsrc = document.getElementById("lightbox-cont-img").getAttribute("src");

      // now match the sequence number in the array 
      var serialofarray = 0;
      for(l=0;l<allimgurlarray.length;l++){
        if(currimgsrc == allimgurlarray[l][2]){
          serialofarray = allimgurlarray[l][0];
        }
      }

      // with RIGHT arrow, we are supposed to increase the sequence and then use its ATTR SRC to LIGHT BOX
      if(serialofarray>=allimgurlarray.length-1){
        serialofarray = 0;
      }
      else {
        serialofarray = serialofarray + 1;
      }
      console.log("Right Arrow : "+serialofarray);
      document.getElementById("lightbox-cont-img").setAttribute("src", allimgurlarray[serialofarray][2]);
    }
  }
  else { // If any key other than ESC is pressed

    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){
      document.getElementById("lightbox-container").classList.remove("showcontainer");
    }
  }
}

// through this we are handling the CLICK ON IMAGE events
document.onclick= function(event) {
  overlaypop(event);
};
function overlaypop(event) {
  if (event===undefined) event= window.event;
  var targettag = event.target;
  var targetparent = event.target.parentNode;
  if(targettag.tagName=='IMG'){
    if(targetparent.nodeName=='A'){
      event.preventDefault();
      var hrefofparent = targetparent.getAttribute("href");
      //alert('clicked on '+ targettag.tagName + ' parent name is ' + targetparent.nodeName + ' and URL is ' + hrefofparent);
      document.getElementById("lightbox-cont-img").setAttribute("src", hrefofparent);
      document.getElementById("lightbox-container").classList.add("showcontainer");
      document.getElementById("lightbox-cont-img").focus();
    }
  }
}
function hideoverlay() {
  document.getElementById('lightbox-container').classList.remove('showcontainer')
}

With the Javascript above, we wish to achieve following purposes.

  1. Popup image on full screen with a translucent black background when any image is Touched, Clicked or Pressed Entered upon after navigating to the desired image using the TAB key.
  2. Hide this Lightbox when Touched, Clicked anywhere on the screen or any key on Keyboard, except Left and Right Arrow keys, is pressed.
  3. Navigate through all the images on a webpage available in the format of with Left and Right arrow key.

Briefly, let us see how this script achieves our purposes by understanding various parts of this Javascript.

  1. With document.querySelectorAll("a[href]"), we wish to get all the IMG tags with Parent A tag in an array named atagswithimgtag.

    • We will use this array, first to disable the default behaviour of opening A tag in a new page by using event.preventDefault().
    • Then we will use this array to create a new 2D array with name allimgurlarray to store the A tag's HREF URLs and their Index number. This allows for a better trace when using the Left and Right keys.
  2. After this, we have to handle two types of events i.e. Key press events and Touch/Click events.

    • Key Press events are handled with document.onkeydown. Here we have to Handle Enter, Tab, Esc, Right and Left arrow keys with the help of If-Else-If conditions.
    • Touch or Click Events are handled with document.onclick.
  3. We use .classList.contains to check if Lightbox is hidden or visible. And we use .classList.add and .classList.remove to show and hide the Lightbox respectively.

  4. We use document.activeElement , .tagName and .childNodes to identify the IMG tag and its parent A tag on which Enter key is pressed after using TAB key for navigation.

  5. And We use window.event, event.target, event.target.parentNode and .nodeName to identify the IMG tag and its parent A tag when the respective image is clicked or touched.

  6. To make the Lightbox more ARIA compatible we use .focus() to bring focus on the image currently displayed in the Lightbox.


Clicking, touching or pressing any key will hide the Lightbox when it is visible.


Check this Answer to learn in details of how this Javascript will handle general Key Press events of keys like ESC, LEFT and RIGHT arrow.

Yaron answered 19/4, 2021 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.