image-zoom
Image zoom on click as seen on popular publishing platform.
You click on an image and it smoothly zooms in or out to fit screen. You click again — it smoothly goes back to normal. You scroll — it also goes back.
npm install mvoloskov/image-zoom --save
or
yarn add mvoloskov/image-zoom
import imageZoom from 'image-zoom'
imageZoom()
<script src="https://cdn.jsdelivr.net/gh/mvoloskov/image-zoom/dist/image-zoom.min.js"></script>
<script>
imageZoom()
</script>
That’s it!
Plugin targets meaningful, content images:
<!-- yes -->
<img src="foo.jpg" alt="Cute kitten" />
<!-- no -->
<img src="bar.jpg" />
<img src="bar.jpg" alt="" />
The default selector is img[alt]:not([alt=""])
.
You can provide your own though:
imageZoom('img, svg') // alternative selector
You can always hack the plugin redefining straightforward CSS:
.image-zoom,
.image-zoom-wrapper::after {
transition-timing-function: ease-in-out;
}
.image-zoom-wrapper::after {
background-color: hotpink;
}
Being called, plugin returns the destroy function that you may call to remove event listeners. It will also remove related styles from <head>
and from images themselves.
const destroy = imageZoom()
// don't need it anymore
destroy()
.image-zoom-wrapper
— element that wraps every image. Mimicks its display
property. We use it to add page background and slightly separate the zoomed image from what is behind..image-zoom-wrapper-zoomed
— the same wrapper but when image is zoomed..image-zoom
— image itself that was processed and is interactive ready to zoom..image-zoom-zoomed
— zoomed image.img
inline styles will be destroyed. Use CSS selectors to stylize images.img
shouldn’t have transforms. If needed, wrap it with a container and apply transforms there instead.:root
’s overflow-x
will be hidden
Enjoy!
Author: mvoloskov
Demo: https://miloslav.website/image-zoom/
Source Code: https://github.com/mvoloskov/image-zoom
#javascript