Git Product home page Git Product logo

yet-another-medium-zoom's Introduction

Yet Another Medium Zoom

codecov David license

YAMZ is a lightbox library heavily inspired by Medium.

It provides an easy way to add smooth, minimalistic, and highly customizable Medium-like zooming to your images.

Usage

To use the library, you simply tell it which images to bind to:

import yamz from "yet-another-medium-zoom";

const $images = [...document.querySelectorAll("img, picture")];
yamz.bind($images);

That's it. The library will automatically extract the appropriate high-res source from your images if you are using srcset, and will otherwise just use the image's own source (or a high-res source you specify).

Configuration

If you want to have more control over what's displayed, you may want to change some of the options. You can set an option globally, or you can specify the options on a per-image basis. This can be done in JavaScript when you open the lightbox, or in HTML by specifying the options on the image you bind to as data attributes.

<!-- set the options as data attributes -->
<img src="example.png" data-scroll-allowance="-1" data-duration="600" />
/* or specify them when you open the lightbox programmatically */
import yamz from "yet-another-medium-zoom";

yamz.setOptions({
    // set options globally
    /* ... */
});
const $img = document.querySelector("img");
yamz.open($img, {
    // set options on a per-image basis
    /* ... */
});

The following options are available:

Name Data attribute Type Default value Description
scrollAllowance data-scroll-allowance number 128 How much the user can scroll before the lightbox is closed. -1 to disable
duration data-duration number 300 How long the animation should take, measured in milliseconds
container None HTMLElement document.body The element to render the lightbox inside
class data-class string null Class to give to the lightbox element. Mostly useful for styling
highres data-highres string null URL of the high-res image to load. Can't be set globally
caption data-caption string | HTMLElement null String or element to insert as a caption below the image. Can't be set globally
album data-album { img: HTMLElement, opts?: {} }[] null If set, the album that an image belongs to. Can be specified in HTML by using a string - all images with the same album string will then be considered part of the album. Can't be set globally
wrapAlbum None boolean false Whether to wrap albums, so you can press left on the first image to go to the last one, and vice versa
lightboxGenerator None (img: HTMLElement, opts: ImageOptions) => HTMLElement null Function which generates the lightbox, if you want to use a custom one. See below for more information

Advanced Customization

There are two main ways to perform more in-depth customization of the lightbox:

CSS / DOM customization

You can style the lightbox however you want by simply using CSS to overwrite the built-in styles. You can also change the DOM structure of the lightbox by using the lightboxGenerator option, which lets you completely change what the lightbox contains. You can see an example of this on the project website. The animation will automatically adapt to your new styles.

Plugins

Plugins can be created if you need more advanced control. These are implemented as functions that augment a given YAMZ instance with whatever features the plugin implements. These plugins can then be composited if you want to use more than one at a time. An example of this can be seen in our main export.

Both the caption and album support are actually implemented as plugins (that are enabled by default), so if you want to create your own plugin it might be useful to reference the implementation of the caption or album plugins.

yet-another-medium-zoom's People

Contributors

birjj avatar dependabot[bot] avatar igregson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

igregson

yet-another-medium-zoom's Issues

Scrollbars are briefly added if opening an image below the viewport

When opening an image that's cutoff by the lower part of the viewport (or that exists entirely below the viewport), the placeholder that gets animated into place causes a scrollbar to appear. This causes the page content to be offset momentarily.

To reproduce: Go to https://yamz.jfagerberg.me/, scroll down until an image is cutoff by the lower half of the viewport (i.e. only a small part of the image is visible, the rest of the image is below the viewport). Click the image. While the animation happens, a scrollbar will appear (or in this case, a secondary scrollbar)

Image showing two scrollbars while an image is being opened

Add option to disable image in/out animations on album prev/next

First off, echoing the experience and sentiment of #3, a huge thank you for this library.


Would love the option to disable the animations on moving between images in an album.

The UX of no transitions is preferable IMO. PhotoSwipe's demo is a good example of this.

Without a custom option for this it does not seem possible. I've tried overriding the CSS but was not able to get this approach to work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.