Webflow Rich Text Lightbox
Example:
"The boy continued to listen to his heart as they crossed the desert. He came to understand its dodges and tricks, and to accept it as it was. He lost his fear, and forgot about his need to go back to the oasis, because, one afternoon, his heart told him that it was happy.
'Even though I complain sometimes,' it said, 'it's because I'm the heart of a person and people's hearts are that way. People are afraid to pursue their most important dreams, because they feel that they don't deserve them, or that they'll be unable to achieve them. We, their hearts, become fearful just thinking of loved ones who go away forever, or of the moments that could have been good but weren't, or of treasures that might have been found but were forever hidden in the sands. Because, when these things happen, we suffer terribly." - The Alchemist
Code
Copy this code and paste into </body> of your page
How it works
1. Defining the replaceImagesWithLightboxes Function: This function is called with an element (in your case, the container of your rich text content). It's responsible for the main functionality of the script.
Here, figures will hold a NodeList of all figure elements within your richtext that have the class w-richtext-figure-type-image.
2. Iterating Over Figures: The script iterates over each figure element found in the previous step. For each figure, it processes the image and its associated caption.
3. Processing Each Image and Caption:
- Finding the Image and Caption: The script looks for an img tag within a div and a figcaption inside the figure. It extracts the necessary attributes from the image and the text from the caption.
Skip if No Image Found: If an image isn't found within the figure, the script skips to the next figure.
4. Creating the Lightbox:
- Lightbox Link: A new <a> element is created, which will act as the lightbox. It's set to open the image URL and is styled with Webflow's lightbox classes.
- Lightbox Image: A new <img> element is created for the lightbox, with attributes like src, alt, and srcset (for responsive images).
- Lightbox Script: A <script> tag of type application/json is created. This contains the data for the lightbox, including the image URL, width, and the caption text.
5. Updating the HTML Structure:
- Clearing and Appending to Image Container: The script clears the imageContainer and appends the newly created lightbox to it.
- Preserving the Caption: If a caption is present, it is moved to immediately follow the figure element. This is crucial because the script then clears the entire figure element and re-adds the imageContainer (now with the lightbox) and the figcaption.
6. Generate Srcset Function: This function creates a srcset attribute value for the lightbox images, enabling responsive image loading based on screen size.
7. Initializing the Script for the Container Element: Finally, the script looks for the container element (.text-rich-text.w-richtext) and calls replaceImagesWithLightboxes on it if the element is found.
In summary, the script finds each image in your richtext, wraps it in a lightbox, and ensures that the captions are preserved and displayed correctly. The figure elements are modified to include this new lightbox functionality while keeping their associated captions in place.