Upon the initial loading of a website, you may notice that the images tend to flicker or flash when transitioning between them. However, once these images are stored in the browser's cache, subsequent visits to the site will display the images seamlessly and without any flashing or delays.
<script lang="ts">
import {onMount} from "svelte";
import {writable} from "svelte/store";
// Specify the URLs for your images here
const images = [
{
path: "https://static.bunnycdn.ru/i/cache/images/5/58/5806a16f2892768b4930c39ebf6ce756.jpg",
id: "Image 1",
},
{
path: "https://static.bunnycdn.ru/i/cache/images/0/03/0307ce050517b8fde68134b23fa9b1d1.jpg",
id: "Image 2",
},
];
let currentIndex = writable(0);
let interval: ReturnType<typeof setInterval>;
onMount(() => {
interval = setInterval(() => {
currentIndex.update((value) => (value + 1) % images.length);
}, 3000); // Change slide every 3 seconds
return () => {
clearInterval(interval);
};
});
</script>
<div class="slideshow">
<div class="list">
{#each images as image, i (image)}
<div
class="item"
style="position: relative; display: {$currentIndex === i ? 'block' : 'none'};"
>
<img
class="slide {i === $currentIndex ? 'active' : ''}"
src={image.path}
alt={`Slide ${i + 1}`}
/>
</div>
{/each}
</div>
</div>
This piece of code showcases my work in action.
My goal is for the website to load smoothly without any flickering when transitioning between images.