simplelightbox - Documentation

Getting Started

Installation

SimpleLightbox can be installed via npm, yarn, or by including the minified CSS and JS files directly in your project.

npm:

npm install simplelightbox

yarn:

yarn add simplelightbox

Direct Download:

Download the minified CSS (simplelightbox.min.css) and JS (simplelightbox.min.js) files from the SimpleLightbox repository or CDN and include them in your project’s HTML file within the <head> (for CSS) and before the closing </body> tag (for JS).

Basic Usage

After installation, initialize SimpleLightbox on your gallery elements. The simplest usage involves selecting your gallery items and calling the SimpleLightbox constructor:

const lightbox = new SimpleLightbox('.gallery a', {
  // Options go here (see below for details)
});

Replace .gallery a with the CSS selector targeting your image links within a container with the class “gallery”. This selector should target <a> tags containing your images, preferably as <img> tags within the <a> tags.

HTML Structure

Your HTML should structure your image gallery as a container (e.g., a <div>) containing links to your images. Each image link should be an <a> tag containing an <img> tag:

<div class="gallery">
  <a href="image1.jpg" data-caption="Image 1 Caption">
    <img src="image1_thumb.jpg" alt="Image 1">
  </a>
  <a href="image2.jpg" data-caption="Image 2 Caption">
    <img src="image2_thumb.jpg" alt="Image 2">
  </a>
  <!-- ...more images... -->
</div>

The href attribute specifies the path to the full-sized image, while the src attribute points to a thumbnail image. The data-caption attribute is optional and provides a caption for the lightbox. Using descriptive alt text for accessibility is crucial.

CSS Integration

Include the SimpleLightbox CSS file in your project’s <head> section. If you used npm or yarn, this might be handled automatically by your bundler; otherwise, add it via a <link> tag:

<link href="simplelightbox.min.css" rel="stylesheet">

Replace "simplelightbox.min.css" with the actual path to your downloaded CSS file. No additional CSS is generally needed for basic functionality, but you can customize the lightbox’s appearance by overriding the default styles. Check the SimpleLightbox documentation for details on customizing styles.

Configuration Options

Overview of Options

SimpleLightbox offers extensive configuration options to customize its behavior and appearance. These options are passed as a single object to the SimpleLightbox constructor. For example:

const lightbox = new SimpleLightbox('.gallery a', {
  /* options here */
});

All options are optional; if not provided, SimpleLightbox will use its default settings.

Image Options

Caption Options

Counter Options

Toolbar Options

Animation Options

Keyboard Navigation Options

Accessibility Options

Advanced Options

Remember to consult the SimpleLightbox documentation for the most up-to-date information on available options and their usage.

API Reference

Opening the Lightbox

You can programmatically open the lightbox by using the show() method. Pass the index (zero-based) of the image you want to open. If no index is specified, it opens the first image in the gallery.

lightbox.show(2); // Opens the third image
lightbox.show();  // Opens the first image

Closing the Lightbox

To close the lightbox, use the close() method:

lightbox.close();

Next/Previous Navigation

SimpleLightbox handles next/previous navigation automatically when the lightbox is open. However, you can trigger these actions programmatically using:

lightbox.next(); // Go to the next image
lightbox.prev(); // Go to the previous image

Methods

Events

SimpleLightbox triggers several events that you can listen for using the standard addEventListener method. These events are dispatched on the SimpleLightbox instance itself.

Example of adding an event listener:

lightbox.on('show.simplelightbox', function() {
  console.log('Lightbox opened!');
});

lightbox.on('closed.simplelightbox', function() {
    // Perform some action after the lightbox is closed
    console.log('Lightbox closed!');
});

//To remove an event listener:
lightbox.off('show.simplelightbox');

Remember to consult the SimpleLightbox documentation for the most up-to-date list of methods and events. The event names might include namespaces for better organization. Check the library’s documentation for accurate naming.

Customization

Customizing the CSS

SimpleLightbox provides a clean and customizable CSS structure. You can modify the default styles by creating your own CSS rules and overriding the existing classes. The main CSS class is simple-lightbox, but you can also target more specific elements using classes like simple-lightbox__nav, simple-lightbox__prev, simple-lightbox__next, and simple-lightbox__wrap. Refer to the SimpleLightbox source code or the generated CSS file for a complete list of classes and their selectors.

For instance, to change the background color of the overlay, you could add the following to your stylesheet:

.simple-lightbox__overlay {
  background-color: rgba(0, 0, 0, 0.7); /* Adjust opacity as needed */
}

Remember that overriding styles should be done with specificity in mind, ensuring your custom CSS targets the correct elements effectively.

Customizing the HTML

While SimpleLightbox doesn’t allow direct manipulation of its internal HTML structure, you can influence its appearance indirectly through CSS styling and the configuration options. For instance, the captionSelector and captionData options allow you to choose where to find caption text, which can be used in conjunction with custom HTML structuring and CSS to alter how the caption is displayed. The counterSelector option allows using a custom counter element.

For more extensive HTML changes, consider creating a custom lightbox implementation.

Extending SimpleLightbox

SimpleLightbox’s architecture allows for extension. Though not directly supported through a plugin system, you can create custom functionality by extending its capabilities within your own code. This could involve adding new methods, modifying existing ones, or attaching custom event listeners.

For example, let’s add a method to SimpleLightbox that centers the lightbox vertically:

// Assuming 'lightbox' is your SimpleLightbox instance
lightbox.centerVertically = function() {
  const lightboxWrapper = this.options.classes.wrap; //Get the wrapper using options
  const lightboxElement = document.querySelector(`.${lightboxWrapper}`);
  if (lightboxElement) {
    const windowHeight = window.innerHeight;
    const lightboxHeight = lightboxElement.offsetHeight;
    const topOffset = (windowHeight - lightboxHeight) / 2;
    lightboxElement.style.top = `${topOffset}px`;
  }
};

//Now you can use it:
lightbox.centerVertically();

This example demonstrates how to add a new method; similar techniques can be used to extend other aspects of SimpleLightbox. Remember to be mindful of SimpleLightbox’s internal workings to avoid conflicts or unexpected behavior when modifying or extending its functionality. Always back up your changes or work within a separate development branch.

Troubleshooting

Common Issues

Debugging Tips

Known Bugs

At the time of writing this, there are no known critical bugs affecting SimpleLightbox. However, always refer to the official SimpleLightbox repository or documentation for the most up-to-date information on known issues and bug fixes. Report any newly discovered bugs or unexpected behaviors to the project maintainers.

Contributing

We welcome contributions to SimpleLightbox! Whether you’re reporting a bug, submitting a pull request, or suggesting improvements, your help is valuable.

Reporting Bugs

When reporting bugs, please provide as much detail as possible to help us understand and reproduce the issue. A good bug report typically includes:

Use the issue tracker on the SimpleLightbox repository to report bugs. Follow the issue template provided for a structured report.

Submitting Pull Requests

Before submitting a pull request, please ensure your code adheres to the coding style guide (see below) and that all tests pass. A good pull request includes:

Fork the SimpleLightbox repository, create a new branch for your changes, and then submit a pull request.

Coding Style Guide

SimpleLightbox follows a consistent coding style to ensure readability and maintainability. Please adhere to these guidelines when contributing:

Following these guidelines ensures a consistent codebase and makes it easier for others to understand and contribute to the project. Before submitting your pull request, make sure to run the provided linting tools (if any exist for the project) to automatically format and verify your code style.

License

License Information

SimpleLightbox is licensed under the MIT License. This means you are free to use, modify, and distribute SimpleLightbox in your projects, both commercial and non-commercial, provided you include the original copyright notice and license. See the LICENSE file in the SimpleLightbox repository for the complete license text.