Ekko Lightbox is a lightweight, responsive, and customizable JavaScript library designed to create beautiful and user-friendly image and video lightboxes. It’s built for ease of use and integration, allowing developers to quickly add a robust lightbox functionality to their websites and applications with minimal code. Ekko Lightbox supports various media types, including images, iframes, and even custom content, offering a flexible solution for diverse content presentation needs.
Ekko Lightbox can be easily installed via npm or a CDN.
Using npm:
npm install ekko-lightbox
Then, import it into your project:
import EkkoLightbox from 'ekko-lightbox';
Using a CDN (e.g., jsDelivr):
Include the following script tag in your HTML <head>
:
<script src="https://cdn.jsdelivr.net/npm/ekko-lightbox@5.3.0/dist/ekko-lightbox.min.js"></script>
Remember to include the appropriate CSS file as well (either from npm or CDN):
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ekko-lightbox@5.3.0/dist/ekko-lightbox.css">
After including the necessary files, initialize Ekko Lightbox:
$(document).ready(function(){
$(document).on('click', '[data-toggle="lightbox"]', function(event) {
event.preventDefault();
$(this).ekkoLightbox();
;
}); })
This example shows how to create a simple image lightbox:
<!DOCTYPE html>
<html>
<head>
<title>Ekko Lightbox Example</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ekko-lightbox@5.3.0/dist/ekko-lightbox.css">
</head>
<body>
<a href="image1.jpg" data-toggle="lightbox" data-gallery="gallery1" data-title="Image 1">
<img src="thumbnail1.jpg" alt="Image 1">
</a>
<a href="image2.jpg" data-toggle="lightbox" data-gallery="gallery1" data-title="Image 2">
<img src="thumbnail2.jpg" alt="Image 2">
</a>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ekko-lightbox@5.3.0/dist/ekko-lightbox.min.js"></script>
<script>
$(document).ready(function(){
$(document).on('click', '[data-toggle="lightbox"]', function(event) {
event.preventDefault();
$(this).ekkoLightbox();
;
});
})</script>
</body>
</html>
Remember to replace "image1.jpg"
, "thumbnail1.jpg"
, etc. with the actual paths to your images. The data-gallery
attribute allows grouping images into galleries for sequential navigation. The data-title
attribute sets the caption for the image. Ensure you have jQuery included, as Ekko Lightbox relies on it.
Ekko Lightbox is primarily initiated through the use of the data-toggle="lightbox"
attribute on HTML elements (typically <a>
tags for images). When a user clicks on an element with this attribute, the lightbox will open, displaying the content specified in the element’s href
attribute. For example:
<a href="image.jpg" data-toggle="lightbox" data-title="My Image">View Image</a>
Alternatively, you can programmatically open the lightbox using jQuery:
$('#myElement').ekkoLightbox();
Replace #myElement
with the appropriate jQuery selector for your element.
The lightbox can be closed by clicking the close button (usually an “X” in the top-right corner), clicking outside the lightbox overlay, or pressing the Escape key. Programmatically, you can close the lightbox using:
.ekkoLightbox.close(); $
This will close any currently open lightbox instance.
Ekko Lightbox offers several options for displaying images within the lightbox. These options can be set using data attributes on the link element or through JavaScript configuration. Key options include:
data-gallery
: Groups multiple images into a gallery for sequential viewing. All elements with the same data-gallery
value will be part of the same gallery.data-title
: Specifies a caption or title to be displayed below the image.data-type
: While Ekko Lightbox automatically detects image types, you can explicitly set data-type="image"
for clarity.data-remote
: This attribute is not specifically for images but is important to note for its use in loading remote content (see Content Loading and Display). If you are loading an image from a remote source via data-remote
, ensure the server returns the correct content-type header.Ekko Lightbox supports video display primarily through iframes. To display a video, use the href
attribute to point to the URL of the video embed code (e.g., a YouTube or Vimeo embed link). For example:
<a href="https://www.youtube.com/embed/yourVideoID" data-toggle="lightbox" data-type="iframe">Watch Video</a>
Note the data-type="iframe"
attribute, which is crucial for Ekko Lightbox to correctly handle the iframe content. The lightbox will handle resizing and displaying the video appropriately within its confines. Consider adding options for autoplay, controls, etc., directly within the embed code from your video hosting platform.
Beyond images and videos, Ekko Lightbox allows you to display custom content. This is primarily done using the data-remote
attribute. This attribute specifies a URL from which the lightbox will load content dynamically. The content at the specified URL should be valid HTML that will be rendered within the lightbox.
<a href="#" data-toggle="lightbox" data-remote="content.html" data-type="ajax">Load Content</a>
In this example, the lightbox will load the content of content.html
via an AJAX request. The data-type="ajax"
attribute tells Ekko Lightbox to handle this as an AJAX request. You can also directly specify HTML content without a remote URL by using the data-html
attribute.
Note: Always ensure that the content loaded via data-remote
or data-html
is appropriately formatted and safe for inclusion in the lightbox. Consider sanitizing any user-supplied content to prevent XSS vulnerabilities.
Ekko Lightbox’s appearance can be customized extensively using CSS. The library provides a set of CSS classes that can be targeted to modify various aspects of the lightbox’s design. The main container for the lightbox is given the class ekko-lightbox
. Within this container, you’ll find classes for the overlay, image container, caption, navigation controls, and more. Consult the Ekko Lightbox CSS file for a complete list of classes and their functionality. You can override these styles in your own CSS file to match your website’s design. For example, to change the background color of the overlay:
.ekko-lightbox .modal-backdrop {
background-color: rgba(0, 0, 0, 0.8); /* Adjust opacity as needed */
}
Remember to include your custom CSS after the Ekko Lightbox CSS file to ensure your styles override the defaults.
While Ekko Lightbox provides default navigation controls (previous/next buttons and a close button), you can customize their appearance and behavior through CSS or, to a lesser extent, JavaScript. CSS allows you to modify the styling of the buttons, changing their size, color, shape, and position. More complex interactions might require JavaScript modifications, but this is generally not recommended unless absolutely necessary as it can lead to conflicts with updates to the library.
Beyond images, iframes, and remotely loaded content, you can incorporate custom HTML content into the lightbox. This is achieved through the data-html
attribute. The value of data-html
should contain the HTML code to be displayed. For example:
<a href="#" data-toggle="lightbox" data-html="<div><h1>My Custom Content</h1><p>This is a paragraph of text.</p></div>">Show Custom Content</a>
This will display a div containing a heading and a paragraph within the lightbox. Remember to sanitize any dynamically generated HTML to prevent security vulnerabilities.
Ekko Lightbox is inherently responsive. It adapts to different screen sizes and devices without requiring specific responsive CSS adjustments. However, you might need to fine-tune the CSS to ensure optimal appearance and behavior across various screen sizes and orientations. Consider using media queries to apply specific styles for different screen widths. For example:
@media (max-width: 768px) {
.ekko-lightbox .modal-dialog {
max-width: 90%; /* Adjust as needed */
} }
This code adjusts the maximum width of the lightbox on screens smaller than 768 pixels.
To enhance the accessibility of your Ekko Lightbox implementation:
<button>
for close buttons, appropriate ARIA attributes for screen readers) within your lightbox content.aria-label
to provide descriptive labels for interactive elements that might lack explicit text labels.By following these guidelines, you can create an accessible and inclusive lightbox experience for all users.
Beyond the basic data attributes, Ekko Lightbox offers robust programmatic control through its JavaScript API. This allows for more complex integration and customization. Key methods include:
$.ekkoLightbox()
: Initializes the lightbox on the selected element(s). Can be used with various options to customize behavior.$.ekkoLightbox.open()
: Programmatically opens a lightbox. Requires an object specifying the content (e.g., href
, type
, gallery
, etc.).$.ekkoLightbox.close()
: Closes the currently active lightbox.$.ekkoLightbox.destroy()
: Removes Ekko Lightbox functionality from an element.Example of programmatic opening:
.ekkoLightbox.open({
$href: 'image.jpg',
title: 'My Image',
gallery: 'myGallery'
; })
Ekko Lightbox triggers several events throughout its lifecycle, allowing you to hook into various stages of the lightbox functionality. These events can be listened for using jQuery’s on()
method. Important events include:
shown.ekkoLightbox
: Triggered after the lightbox is fully shown and visible.hidden.ekkoLightbox
: Triggered after the lightbox is closed and hidden.loading.ekkoLightbox
: Triggered when the lightbox starts loading content.error.ekkoLightbox
: Triggered if an error occurs during content loading.Example of handling the shown.ekkoLightbox
event:
$(document).on('shown.ekkoLightbox', function (e) {
console.log('Lightbox shown:', e.target);
; })
Ekko Lightbox can be integrated with various other JavaScript libraries. However, ensure compatibility. Potential conflicts might arise, especially when dealing with other modal or overlay libraries. Thorough testing is crucial when combining Ekko Lightbox with other frameworks or plugins. Properly managing event handling and ensuring no conflicting selectors are used is essential for successful integration.
Ekko Lightbox provides the error.ekkoLightbox
event to handle errors during content loading. This event is triggered if the lightbox fails to load the specified content (e.g., due to a 404 error or invalid URL). You can use this event to display a user-friendly error message or take alternative actions.
$(document).on('error.ekkoLightbox', function (e) {
alert('Error loading content: ' + e.message);
; })
Make sure to handle potential errors gracefully to provide a better user experience.
For optimal performance, consider these points:
By following these optimization techniques, you can improve the performance and responsiveness of Ekko Lightbox in your application.
The EkkoLightbox constructor is primarily invoked implicitly through the data-toggle="lightbox"
attribute on HTML elements or explicitly via the $.ekkoLightbox()
method. It does not accept arguments directly in this way. Configuration options are provided via data attributes on the element or as an options object passed to $.ekkoLightbox()
. These options determine the lightbox’s behavior. Refer to the options section below for a detailed list of available settings.
The EkkoLightbox plugin exposes the following methods via jQuery:
$.ekkoLightbox()
: Initializes the lightbox on the selected element(s). This is the primary way to activate the lightbox functionality. It accepts an optional options object to customize its behavior.
$.ekkoLightbox.open(options)
: Programmatically opens a lightbox instance. options
is an object that accepts the same options as $.ekkoLightbox()
, including href
, type
, gallery
, and other settings.
$.ekkoLightbox.close()
: Closes the currently active lightbox instance.
$.ekkoLightbox.destroy(element)
: Removes Ekko Lightbox functionality from the specified element
(a jQuery object or selector). This reverses the initialization, removing event handlers and associated data.
Ekko Lightbox triggers several custom events that can be listened for using jQuery’s on()
method. These events provide hooks for extending functionality or customizing behavior:
shown.ekkoLightbox
: Triggered when the lightbox has been fully displayed. The event object contains a target
property referencing the lightbox element.
hidden.ekkoLightbox
: Triggered when the lightbox has been closed and hidden.
loading.ekkoLightbox
: Triggered when the lightbox begins loading content (e.g., an image or remote content).
error.ekkoLightbox
: Triggered if an error occurs during content loading. The event object may contain an error
property with details about the error.
swap.ekkoLightbox
: Triggered when the lightbox switches to a different item within a gallery.
To listen for these events, use:
$(document).on('shown.ekkoLightbox', function(e) { /* your code */ });
Ekko Lightbox doesn’t directly expose public properties in the traditional sense. Its state and configuration are largely managed internally. However, information about the currently active lightbox (if any) can be accessed indirectly through inspecting the DOM elements that are created and modified by Ekko Lightbox. For example, you could access the current image URL by inspecting the src
attribute of the image element within the lightbox modal. This approach is not recommended for general use, as it relies on the internal implementation details that could change in future versions of the library. It’s best to leverage the provided events and methods for interacting with the lightbox’s behavior and state.
data-toggle="lightbox"
attribute and a valid href
attribute.href
attributes, or server-side issues (e.g., 404 errors).$.ekkoLightbox.close()
.Browser Developer Tools: Use your browser’s developer tools (usually accessed by pressing F12) to inspect the HTML, CSS, and JavaScript. Check for errors in the console and use the network tab to monitor requests.
Console Logging: Add console.log()
statements to your JavaScript code to track the execution flow and variable values. This can help identify where problems occur.
Simplify: If you have complex customizations, try simplifying your code to isolate the source of the problem. Test with a minimal setup to see if the issue persists.
Check for Conflicts: If using other JavaScript libraries, check for potential conflicts with Ekko Lightbox’s functionality.
Ekko Lightbox is designed to work across modern browsers. However, older or less common browsers might experience compatibility issues. Thorough testing across various browsers (Chrome, Firefox, Safari, Edge) and versions is recommended.
data-title
attribute on the link element.data-type="iframe"
. Make sure the href
attribute points to the video embed code URL.$.ekkoLightbox.open({ /* options */ })
The options
object should include the content to display (e.g., href
, type
, gallery
).If you have further questions or encounter issues not covered here, please refer to the project’s GitHub repository or community forums for additional support and information.
We welcome contributions to Ekko Lightbox! Whether you’re fixing bugs, adding features, or improving the documentation, your help is valuable. Please follow these guidelines to ensure a smooth and efficient contribution process.
When reporting bugs, please provide as much detail as possible to help us understand and reproduce the issue. A clear and concise bug report should include:
Feature suggestions are always welcome! When suggesting a new feature, please:
fix-bug-123
, feature-new-gallery
).To maintain consistency and readability, please follow these coding style guidelines when contributing to Ekko Lightbox:
Before submitting a pull request, ensure your code passes any automated tests and adheres to these guidelines. We appreciate your cooperation in making Ekko Lightbox a well-maintained and high-quality project.