proper fancy image loading for buttons

This commit is contained in:
cirroskais 2024-04-20 22:10:19 -04:00
parent 315b7576b4
commit 94bbfe98ef
No known key found for this signature in database
GPG key ID: 5FC73EBF2678E33D
2 changed files with 22 additions and 14 deletions

View file

@ -1,12 +1,13 @@
<script>
import { onMount } from 'svelte';
import { fade } from 'svelte/transition';
import Eye from './Icons/Eye.svelte';
/** @type {string} src */
export let src;
/** @type {string=} href */
export let href;
/** @type {string|undefined} href */
export let href = undefined;
/** @type {HTMLImageElement} e */
let e;
@ -27,22 +28,29 @@
e.style.boxShadow = `${(-3 * xP).toFixed(2)}px ${(5 * yP).toFixed(2)}px 3px #c4bef3`;
};
if (src) {
const preload = new Image();
preload.src = src;
preload.onload = () => {
loaded = true;
e.addEventListener('mouseover', startEffect);
e.addEventListener('mousemove', startEffect);
e.addEventListener('mouseleave', () => {
e.style.transform = '';
e.style.boxShadow = '';
});
const preload = new Image();
preload.src = src;
preload.onload = () => (loaded = true);
};
}
});
</script>
<a class="hover:shadow-2xl" {href} target="_blank">
<img in:fade class="imgbutton {!loaded && 'hidden'}" {src} alt="Button" bind:this={e} />
<div class="animate-pulse imgbutton bg-neutral-600/50 rounded-sm {loaded && 'hidden'}"></div>
{#if loaded}
<img in:fade class="imgbutton" {src} alt="Button" bind:this={e} />
{:else}
<div class="rounded-sm animate-pulse imgbutton bg-neutral-600/50"></div>
{/if}
</a>
<style lang="postcss">

View file

@ -92,7 +92,7 @@
<ImgButton href="https://split.pet" src="/buttons/split.png"></ImgButton>
<ImgButton href="https://www.mozilla.org/en-US/firefox/new/" src="/buttons/firefox.gif"
></ImgButton>
<ImgButton src="/buttons/lol.gif" href=""></ImgButton>
<ImgButton src="/buttons/lol.gif"></ImgButton>
</div>
</div>
</div>