miloxeon / fast-image-zoom

🏞 Fast image zoom on click as seen on a popular publishing platform
https://miloslav.website/fast-image-zoom
Boost Software License 1.0
42 stars 3 forks source link

Dependencies npm License

Demo   •   Configuration

🏞 Fast Image Zoom

Image zoom on click as seen on the popular publishing platform.

What does it do?

You click on an image and it smoothly zooms in or out to fit the screen. You click again β€” it smoothly goes back to normal. You scroll β€” it also goes back.

Why is it better than alternatives?

Basic usage

npm install fast-image-zoom --save

or

yarn add fast-image-zoom
import imageZoom from 'fast-image-zoom'
imageZoom()

Alternative β€” use CDN

<script src="https://unpkg.com/fast-image-zoom@7.0.1/dist/fast-image-zoom.js"></script>
<script>
  imageZoom()
</script>

That's it!

How it works

Plugin targets meaningful, content images:

<!-- yes -->
<img src="https://github.com/miloxeon/fast-image-zoom/raw/master/foo.jpg" alt="Cute kitten" />

<!-- no -->
<img src="https://github.com/miloxeon/fast-image-zoom/raw/master/bar.jpg" />
<img src="https://github.com/miloxeon/fast-image-zoom/raw/master/bar.jpg" alt="" />

Configuration

Here are the defaults:

imageZoom({
    selector: `img[alt]:not([alt=""]):not([data-image-zoom-disabled])`,
    containerSelector: null,
    cb: () => {},
    exceed: false,
    padding: 20,
})

Note that if exceed is false and smaller images appear to have a larger gap between their edges and the edge of the viewport, padding won't be added. For example, if you zoom a 100x100 image on a 1080p screen and your padding is set to 20, a natural gap between an image and the viewport edge would be (1080 - 100) / 2 = 490, thus there is no need to add that 20px gap.

Only pixels are supported by now.

Setting exceed per image

You can explicitly define exceed for a specific picture via a data attribute:

<img src="https://github.com/miloxeon/fast-image-zoom/raw/master/.." alt="..." data-image-zoom-exceed="true">

Disabling the plugin for the specific image

You can disable zooming for any image you like, even if it has alt:

<img src="https://github.com/miloxeon/fast-image-zoom/raw/master/.." alt="..." data-image-zoom-disabled>

Note that if you redefine the selector in a way that doesn't account data-image-zoom-disabled attribute, this feature will stop working.

Restyling

You can always hack the plugin redefining straightforward CSS:

Changing a timing function

.image-zoom,
.image-zoom-wrapper::after {
    transition-timing-function: ease-in-out;
}

Changing the background color

.image-zoom-wrapper::after {
    background-color: hotpink;
}

For now, !important might be needed, as styles are injected into <head>. This will probably be changed in the future.

Anatomy

Disabling the plugin

Being called, the plugin returns the destroy function that you may call to remove event listeners. It will also remove related styles from <head> and from the images themselves.

const destroy = imageZoom()

// don't need it anymore
destroy()

Limitations

Enjoy!