diff --git a/src/components/Visible/Visible.svelte b/src/components/Visible/Visible.svelte index 6582e4eb..1cd44968 100644 --- a/src/components/Visible/Visible.svelte +++ b/src/components/Visible/Visible.svelte @@ -45,8 +45,24 @@ }: Props = $props(); let visible = $state(false); + let visibleOnce = $state(false); let container: HTMLElement | undefined = $state(undefined); + function scrollHandler() { + // Only trigger if `visibleOnce` is false + if (container && !visibleOnce) { + const bcr = container.getBoundingClientRect(); + visible = + bcr.bottom + bottom > 0 && + bcr.right + right > 0 && + bcr.top - top < window.innerHeight && + bcr.left - left < window.innerWidth; + + // If `once` is true, set `visibleOnce` to true once `visible` becomes true + if (once && visible) visibleOnce = true; + } + } + onMount(() => { if (typeof IntersectionObserver !== 'undefined') { const rootMargin = `${top}px ${right}px ${bottom}px ${left}px`; @@ -63,31 +79,24 @@ threshold, } ); + if (container) observer.observe(container); + // Unobserve when the component is unmounted return () => { if (container) observer.unobserve(container); }; } - function handler() { - if (container) { - const bcr = container.getBoundingClientRect(); - - visible = - bcr.bottom + bottom > 0 && - bcr.right + right > 0 && - bcr.top - top < window.innerHeight && - bcr.left - left < window.innerWidth; - } - if (visible && once) { - window.removeEventListener('scroll', handler); - } - } - window.addEventListener('scroll', handler); - return () => window.removeEventListener('scroll', handler); }); -
+ + +
{#if children} {@render children(visible)} {/if} diff --git a/src/components/Visible/demo/VisibleDemo.svelte b/src/components/Visible/demo/VisibleDemo.svelte index 04325678..919758c8 100644 --- a/src/components/Visible/demo/VisibleDemo.svelte +++ b/src/components/Visible/demo/VisibleDemo.svelte @@ -5,24 +5,28 @@ import FeaturePhoto from '../../FeaturePhoto/FeaturePhoto.svelte'; import sharkSrc from '../../FeaturePhoto/images/shark.jpg'; + let top = 200; + const demoText = - "Adjust the window dimension to see what happens when `
` comes into Internsection Observer's view."; + 'Take a look at the *Elements* tab in Inspector to see how the photo in the middle of the page appears in the DOM only when its container comes into view.'; + + let demoText2 = $derived( + `The top value for \`rootMargin\` is set to \`${top}px\` in this demo. Read about how \`rootMargin\` affects the Intersection Observer's behaviour here.` + ); const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; - - let top = 500; + + + - - + {#snippet children(visible)} {#if visible} {:else} - -

Not yet visible.

-
+

Not yet visible.

{/if} {/snippet}
- - + + +