NuxtImage Fade In
Hello!
Can someone explain to me how I can add a basic fade effect after the image has been loaded?
I tried this
However, this works on first sight. But when I am switching pages back and forth. Images won't load again. It looks like as if cached images are not firing the load event anymore?
2 Replies
Hi @tierra. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To add a basic fade effect after the image has been loaded using NuxtImg, you're on the right track. However, you've encountered an issue with cached images not triggering the load event when navigating between pages. Let's address this and provide a solution.
First, your approach of using the
@load
event and a reactive variable to control the opacity is correct. However, as you've noticed, cached images might not trigger the load event on subsequent page visits.
To solve this, we can leverage the placeholder
prop of NuxtImg, which will ensure the fade effect works consistently, even with cached images. Here's how you can modify your code:
Key changes and explanations:
1. We've added the placeholder
prop to NuxtImg. This ensures that a placeholder is displayed while the image is loading, even for cached images NuxtImg placeholder (https://image.nuxt.com/usage/nuxt-img#placeholder).
2. We've modified the class binding to use Vue's dynamic class syntax, which is more reliable than data attributes for this use case.
3. The loading="lazy"
attribute is kept, which is good for performance NuxtImg loading (https://image.nuxt.com/usage/nuxt-img#loading).
4. We're still using the @load
event to trigger the fade-in effect NuxtImg events (https://image.nuxt.com/usage/nuxt-img#events).
This approach should work consistently, even when navigating between pages, because the placeholder ensures that the image loading process is triggered each time, regardless of whether the image is cached or not.
If you still encounter issues with this approach, you might want to consider using the useImage()
composable to generate a placeholder, which gives you more control over the process:
This method gives you more control over the placeholder generation and should ensure the fade effect works consistently across page navigations NuxtImg placeholder with useImage (https://image.nuxt.com/usage/nuxt-img#placeholder).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@910553251949195276> Kapa.ai is still learning and improving, please let me know how I did by reacting below.