Animate CSS Background Position With the useSpring Hook

Share this video with your friends

Send Tweet

In this lesson, you'll learn how to implement smooth image scrolling using React Spring's useSpring hook

You will use an onMouseMove event to get the image and mouse coordinates and set useSpring with the offset. You can then style the animated element with the backgroundPositionX and backgroundPositionY CSS properties.

Craig
Craig
~ 4 years ago

Can you please explain the dividing by 4 for the 400 x 400 container?

Lucas Minter
Lucas Minter
~ 4 years ago

@craig, I believe he is using division because it is supposed to be constantly updated. If he hard coded the values, the image wouldn't move. Dividing by 4 splits the 400 x 400 image perfectly. If you were to do a 500 x 500 image, you would divide it by 5. I believe the reason is to get it down to a 100 x 100.

Craig
Craig
~ 4 years ago

got it, thanks!