Drift adds easy "zoom on hover" functionality to your site's images, all with lightweight, no-dependency JavaScript.
Installation
- NPM:
npm install drift-zoom
- Bower:
bower install drift
- Manual: Download and use
dist/Drift.min.js
or dist/Drift.js
If you're using the pre-built version of Drift, it will automatically make window.Drift
available for your use when included on your page.
If you prefer to use require
statements and a build tool like Browserify, here are a couple examples to help:
var Drift = require('drift-zoom');
new Drift(…);
If your project uses ES6, you can do the following instead:
import Drift from 'drift-zoom';
new Drift(…);
Basic Usage
Once you've installed Drift via one of the above methods, you're ready to get started. There are no dependencies, so you can just start making cool stuff. Check out the announcement blog post.
Here's an example of a basic implementation:
<img src="https://assets.imgix.net/dog.png?w=400" data-zoom="https://assets.imgix.net/dog.png?w=1200">
<p>This is a simple description of the dog picture.</p>
new Drift(document.querySelector("img"), {
paneContainer: document.querySelector("p")
});
Demo 💻💻💻
Take a peek at our Demo Site.
Options / Defaults
Here's an example of using Drift with a custom configuration. All of the listed options are displayed with their default value.
var options = {
namespace: null,
showWhitespaceAtEdges: false,
containInline: false,
inlineOffsetX: 0,
inlineOffsetY: 0,
inlineContainer: document.body,
sourceAttribute: 'data-zoom',
zoomFactor: 3,
paneContainer: document.body,
inlinePane: 375,
handleTouch: true,
onShow: null,
onHide: null,
injectBaseStyles: true,
hoverDelay: 0,
touchDelay: 0,
hoverBoundingBox: false,
touchBoundingBox: false,
boundingBoxContainer: document.body,
passive: false,
};
new Drift(document.querySelector('img'), options);
API
Drift#disable
Disable your Drift instance. This will prevent your Drift instance from showing, but will not hide it if it's currently visible.
var drift = new Drift(document.querySelector("img"), {
paneContainer: document.querySelector("p")
});
document.querySelector(".disable-button").addEventListener("click", function() {
drift.disable();
});
Drift#enable
Enable your Drift instance.
var drift = new Drift(document.querySelector("img"), {
paneContainer: document.querySelector("p")
});
document.querySelector(".enable-button").addEventListener("click", function() {
drift.enable();
});
Drift#setZoomImageURL(imageURL)
Change the URL of the zoom image to the passed string. This only has a visible effect while your Drift is currently open. When opening, Drift always pulls the zoom image URL from the specified sourceAttribute
. If you want to make a "permanent" change that will persist after the user leaves and re-enters your Drift trigger, you update its sourceAttribute
as well (default data-zoom
). For more information about this method, please see issue #42.
var triggerEl = document.querySelector("img");
var drift = new Drift(triggerEl, {
paneContainer: document.querySelector("p")
});
var frames = ["https://mysite.com/frame1.jpg", "https://mysite.com/frame2.jpg", "https://mysite.com/frame3.jpg"];
var currentFrame = 0;
setInterval(function() {
currentFrame++;
if (currentFrame > frames.length - 1) {
currentFrame = 0;
}
drift.setZoomImageURL(frames[currentFrame]);
triggerEl.setAttribute("data-zoom", frames[currentFrame]);
}, 1200);
Theming
By default, Drift injects an extremely basic set of styles into the page. You will almost certainly want to extend these basic styles for a prettier, more usable experience that matches your site. There is an included basic theme that may meet your needs, or at least give a good example of how to build out your own custom styles. The namespace
option can be used as a way to easily apply different themes to specific instances of Drift.
If you need to do something very out of the ordinary, or just prefer to include the default styles in CSS yourself, you can pass injectBaseStyles: false
when instantiating a new instance of Drift. Please note that if you disable the included base styles, you will still need to provide an animation for .drift-window.drift-opening
and .drift-window.drift-closing
(this can be a "noop" style animation, as seen in the base styles source).
FAQs/Examples
In this section we answer common questions about Drift.
Disabling on Mobile
If you would like the touch events not to fire on mobile for one reason or another, these two solutions should work for you.
CSS Solution (Recommended)
This solution places a transparent element over the image on mobiles to block touch events. Replace 1024px
in the media query with your mobile breakpoint.
.zoom-image {
position: relative;
}
.zoom-image::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: transparent;
}
@media only screen and (min-width: 1024px) {
.zoom-image::before {
display: none;
}
}
JS Solution
This solution creates and destroys the Drift instance when the browser size changes. It depends on the library responsive.js but can easily be altered to use vanilla JS.
const driftOptions = {
paneContainer: paneContainer,
inlinePane: false,
handleTouch: false
};
const handleChange = () => {
requestAnimationFrame(() => {
if (Responsive.is('mobile') && !!window.productZoom) {
window.productZoom.destroy();
} else {
window.productZoom = new Drift(img, driftOptions);
}
})
}
window.addEventListener('resize', handleChange);
window.addEventListener('load', handleChange);
Use Drift with Multiple Images on the Same Page
This code will iterate over all elements on your page with the class drift-img
, and will instantiate Drift for each element. You can update the query selector and pane as you see fit.
const driftImgs = document.querySelectorAll('.drift-img');
const pane = document.querySelector('.drift-pane');
driftImgs.map(img => {
new Drift(img, {
paneContainer: pane,
inlinePane: false
});
});
Browser Support
We support the latest version of Google Chrome (which automatically updates whenever it detects that a new version of the browser is available). We also support the current and previous major releases of desktop Firefox, Internet Explorer, and Safari on a rolling basis. Mobile support is tested on the most recent minor version of the current and previous major release for the default browser on iOS and Android (e.g., iOS 9.2 and 8.4). Each time a new version is released, we begin supporting that version and stop supporting the third most recent version.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Meta
Drift was made by imgix. It's licensed under the BSD 2-Clause license (see the license file for more info). Any contribution is absolutely welcome, but please review the contribution guidelines before getting started.
License