Implementing a Lightbox for ACF Images in Your WordPress Site

Easy

CSS, Javascript

Machine

Details

Divi Machine’s ACF Item module allows you to display ACF fields on the frontend of your WordPress Divi website. But how can you add a lightbox feature to an ACF image displayed through the ACF Item module? The solution involves CSS, JavaScript, and a few easy steps.

Thanks to Web Dev Simplified for this snippet.

Snippets Demo

Code

1

Description:
Navigate to the ACF Item module on your Single Post page (possibly in the Theme Builder). In the Advanced Tab, go to Custom CSS and add the following code. You can customize this CSS to suit your needs.

Language: CSS

Where to add: Divi Module Advanced Tab

                        /* Style for the lightbox container */
#lightbox {
  /* Fix the lightbox position to the viewport */
  position: fixed;
  /* Ensure the lightbox is above other elements */
  z-index: 1000;
  /* Set the top offset to 100px from the top */
  top: 100px;
  /* Make the lightbox take the full width of the viewport */
  width: 100%;
  /* Make the lightbox take the full height of the viewport */
  height: 100%;
  /* Set the background color to a semi-transparent black */
  background-color: rgba(0, 0, 0, .8);
  /* Hide the lightbox by default */
  display: none;
}

/* Style for the lightbox when it is active (visible) */
#lightbox.active {
  /* Display the lightbox as a flex container */
  display: flex;
  /* Center content horizontally */
  justify-content: center;
  /* Center content vertically */
  align-items: center;
}

/* Style for the image inside the lightbox */
#lightbox img {
  /* Set the image width to 60% of its container */
  width: 60%;
  /* Ensure the maximum width of the image is also 60% */
  max-width: 60%; 
}
                    
2

Description:
Add a Code Module to the page and insert the following JavaScript. Ensure that this Code Module is placed below the ACF Item module from Step One and within the same Divi row.

Language: Javascript

Where to add: Divi Code Module

                        // Create a new div element for the lightbox
const lightbox = document.createElement('div')
// Set the id of the new div to 'lightbox'
lightbox.id = 'lightbox'
// Append the lightbox div to the body of the document
document.body.appendChild(lightbox)

// Select all image elements on the page
const images = document.querySelectorAll('img')

// Loop through each image and add a click event listener
images.forEach(image => {
  image.addEventListener('click', e => {
    // Add the 'active' class to the lightbox when an image is clicked
    lightbox.classList.add('active')
    
    // Create a new image element to display in the lightbox
    const img = document.createElement('img')
    // Set the source of the new image to be the same as the clicked image
    img.src = image.src
    
    // Remove any existing children (images) from the lightbox
    while (lightbox.firstChild) {
      lightbox.removeChild(lightbox.firstChild)
    }
    
    // Append the new image to the lightbox
    lightbox.appendChild(img)
  })
})

// Add a click event listener to the lightbox
lightbox.addEventListener('click', e => {
  // If the target of the click is not the lightbox itself, return early
  if (e.target !== e.currentTarget) return
  
  // Remove the 'active' class from the lightbox to hide it
  lightbox.classList.remove('active')
})
                    

Related Snippets

Explore more from Divi Engine