Created
January 27, 2025 12:55
-
-
Save Kristjan-Reinsberg/034541228a02cee0952670027537f9a3 to your computer and use it in GitHub Desktop.
SVELTE 5 - carousel - no css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script> | |
| import { onMount } from "svelte"; | |
| let { posts } = $props(); | |
| let current = $state(0); | |
| let interval; | |
| onMount(() => { | |
| interval = setInterval(() => { | |
| current = (current + 1) % posts.length; | |
| }, 8000); | |
| }); | |
| function next() { | |
| current = (current + 1) % posts.length; | |
| } | |
| function prev() { | |
| current = (current - 1 + posts.length) % posts.length; | |
| } | |
| </script> | |
| <section title="carousel"> | |
| <div class="relative"> | |
| {#each posts as post, index} | |
| <div | |
| style=" | |
| position: absolute; | |
| top: 0; | |
| left: 0; | |
| width: 100%; | |
| height: 100%; | |
| opacity: {index === current ? 1 : 0}; | |
| visibility: {index === current ? 'visible' : 'hidden'}; | |
| transition: opacity 0.5s ease-in-out, visibility 0.5s ease-in-out; | |
| " | |
| > | |
| <img src={post} alt="screenshot" /> | |
| {index} | |
| </div> | |
| {/each} | |
| </div> | |
| <div class="relative"> | |
| <button onclick={prev}>Previous</button> | |
| <button onclick={next}>Next</button> | |
| </div> | |
| </section> | |
| <!-- USAGE EXAMPLE: | |
| <YourSlider posts="{[ | |
| "https://placehold.co/600x400", | |
| "https://placehold.co/600x400", | |
| ]}"></YourSlider> | |
| --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment