1
0

Добавлен компонент Image

This commit is contained in:
2025-09-11 14:36:13 +04:00
parent 3e143b36ee
commit db83721ab2

View File

@@ -0,0 +1,73 @@
<noscript>
<img
{loading}
{...others}
/>
</noscript>
{#if dynamic}
<img
{loading}
{...others}
bind:this={node}
class="dynamic"
class:visible
/>
{/if}
<script>
let {
loading = 'lazy',
...others
} = $props()
let node = $state()
let visible = $state(false)
let dynamic = $state(false)
$effect(() => {
dynamic = true
})
$effect(() => {
if (!node) {
return
}
if (node.complete) {
visible = true
} else {
node.addEventListener('load', () => visible = true, { once: true })
}
})
</script>
<style lang="scss">
/*
Ideally, <noscript> could use display: contents so it wouldn't create a block container.
However, due to a Chrome bug, we must set it to display: block to ensure proper layout.
This ensures that fallback images inside <noscript> scale correctly to the container width.
https://issues.chromium.org/issues/41179501
*/
noscript,
img {
display: block;
max-width: 100%;
width: 100%;
height: auto;
}
img {
object-fit: cover;
object-position: top center;
&.dynamic {
transition: opacity 0.4s ease;
opacity: 0;
&.visible {
opacity: 1;
}
}
}
</style>