jscottsmith / react-scroll-parallax

🔮 React hooks and components to create parallax scroll effects for banners, images or any other DOM elements.
https://react-scroll-parallax.damnthat.tv/
MIT License
2.9k stars 158 forks source link

onLoad function does not fire on image layers #231

Closed lexwebb closed 1 year ago

lexwebb commented 1 year ago

Bug

I'm attempting to create a loading animation for when my images have downloaded to the client. I'm doing this to nicely transition from the low res fallback image to the high res images i'm using for parallax.

Unfortunately the onLoad function that seems to exist in the types for a layer does not ever seem to get called, which means my images never get style updates onLoad.

Here's a codesandbox demonstrating https://codesandbox.io/s/react-scroll-parallax-template-forked-3x9x83?file=/src/App.tsx

jscottsmith commented 1 year ago

@lexwebb Thanks for the Codesandbox.

The reason your onLoad event isn't firing is because the image in ParallaxBanner uses a div with a background image set to cover.

Screenshot 2023-11-04 at 10 14 27 PM

If you need an img element with a load event handler you can see how to set that up in the documentation

Keep in mind you will also need to style the image to work for your use case, probably with object-fit: cover. Hope that helps.