Moving Backgrounds With Mouse Position
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:
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…
The post Moving Backgrounds With Mouse Position appeared first on CSS-Tricks.