Let’s say you wanted to move the background-position
on an element as you mouse over it to give the design a little pizzazz. You have an element like this:
<div class="module" id="module"></div>
And you toss a background on it:
.module {
background-image: url(big-image.jpg);
}
You can adjust the background-position
in JavaScript like this:
const el = document.querySelector("#module");
el.addEventListener("mousemove", (e) => {
el.style.backgroundPositionX = -e.offsetX + "px";
el.style.backgroundPositionY = -e.offsetY + "px";
});
See the Pen Move a background with mouse by Chris Coyier (@chriscoyier) on CodePen.
Or, you could update CSS custom properties in the JavaScript instead:
const el = document.querySelector("#module");
el.addEventListener("mousemove", (e) => {
el.style.setProperty('--x', -e.offsetX + "px");
el.style.setProperty('--y', -e.offsetY + "px");
});
.module {
--x: 0px;
--y: 0px;
background-image: url(large-image.jpg);
background-position: var(--x) var(--y);
}
See the Pen Move a background with mouse by Chris Coyier (@chriscoyier) on CodePen.
Here’s an example that moves the background directly in JavaScript, but with a transition applied so it slides to the new position rather than jerking around the first time you enter:
See the Pen Movable Background Ad by Chris Coyier (@chriscoyier) on CodePen.
Or, you could move an actual element instead (rather than the background-position
). You’d do this if there is some kind of content or interactivity on the sliding element. Here’s an example of that, which sets CSS custom properties again, but then actually moves the element via a CSS translate()
and a calc()
to temper the speed.
See the Pen Hotjar Moving Heatmap Ad by Chris Coyier (@chriscoyier) on CodePen.
I’m sure there are loads of other ways to do this — a moving SVG viewBox, scripts controlling a canvas, webGL… who knows! If you have some fancier ways to handle this, link ’em up in the comments.
Would be interested in a mobile-friendly solution.
Like using the accelerometer? http://www.albertosarullo.com/blog/javascript-accelerometer-demo-source
I probably should have done a version that also works with the
touchmove
event. Forks welcome!Yeah, a touch-friendly solution would be appreciated. Using the accelerometer seems like too much trial-and-error to levy upon a poor users who’s just trying to tap and drag.
How is that on performance? Would this need a reasonable debounce?
Maybe? I’d worry that with a debounce it would get choppy though. I wonder if there is some way to only update the values within a
requestAnimationFrame
or something. On my computer I don’t see any slowness, but I think general good advice is that DOM events that fire super fast (likemousemove
does) should have some kind of performance handling.Some years ago I saw PC Gamer do something similar. I decided to try using CSS only to make the image appear to move, with JS used
as a convenience to create a grid of empty elements rather than hard-coding them:
I adjusted margins for the “apparent” background-image, but the pen could just as easily have been used to adjust the background-position of a background image. The more empty elements created, the smoother the animation would appear.
hii chris, i wanted to build image zooming when you hover over image and zoomed version showed on side div. like they have in ecommerce site. how can i do that? any suggestion?
The last example don’t work on Chrome on Windows