Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component not compatible with view transitions. #2

Open
wrux opened this issue Oct 9, 2023 · 0 comments
Open

Component not compatible with view transitions. #2

wrux opened this issue Oct 9, 2023 · 0 comments

Comments

@wrux
Copy link

wrux commented Oct 9, 2023

Problem

LQIP images are handled on initial page load. When using the view transition API, images are loaded into the DOM after page load. This resulted in images loading, but not fading in.

Solution

Astro fires an event astro:after-swap, so the on load event can be bound to new images.

Here's a temporary script I added to my app to get images working:

<script>
  document.addEventListener('astro:after-swap', () => {
    document
      .querySelectorAll<HTMLImageElement>('img[data-x-lqip="true"]')
      .forEach((img) => {
        img.addEventListener('load', () => {
          img.animate([{ opacity: 0 }, { opacity: 1 }], {
            duration: parseInt(img.dataset['xLqipTransitionDuration']!),
            fill: 'forwards',
          });
        });
        if (img.complete) {
          img.style.opacity = '1';
        }
      });
  });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant