{"id":357192,"date":"2021-11-19T07:13:46","date_gmt":"2021-11-19T15:13:46","guid":{"rendered":"https:\/\/css-tricks.com\/?p=357192"},"modified":"2021-11-19T07:13:49","modified_gmt":"2021-11-19T15:13:49","slug":"parallax-powered-by-css-custom-properties","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/parallax-powered-by-css-custom-properties\/","title":{"rendered":"Parallax Powered by CSS Custom Properties"},"content":{"rendered":"\n

Good friend Kent C. Dodds has recently dropped his new website<\/a> which had a lot of work go into it. I was fortunate enough that Kent reached out a while back and asked if I could come up with some “whimsy” for the site. ✨<\/p>\n\n\n\n

One of the first things that drew my attention was the large image of Kody (🐨) on the landing page. He’s surrounded by objects and that, to me, screamed, “Make me move!”<\/p>\n\n\n\n\n\n\n\n

\"Life-like<\/figure>\n\n\n\n

I have built parallax-style scenes before that respond to cursor movement, but not to this scale and not for a React application. The neat thing about this? We can power the whole thing with only two CSS custom properties.<\/p>\n\n\n\n


\n\n\n\n

Let’s start by grabbing our user’s cursor position. This is as straightforward as:<\/p>\n\n\n\n

const UPDATE = ({ x, y }) => {\n  document.body.innerText = `x: ${x}; y: ${y}`\n}\ndocument.addEventListener('pointermove', UPDATE)<\/code><\/pre>\n\n\n\n