Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The awesome carousel component for Svelte 3 ⬅️ 🎨 ➡️

License

NotificationsYou must be signed in to change notification settings

vadimkorr/svelte-carousel

Repository files navigation

svelte-carousel

svelte-carousel

npmnpm
GitHub repoGitHub followers

The awesome carousel component for Svelte 3

Demo

Installation

yarn add svelte-carousel
npm install svelte-carousel

Import component

<script>  import Carousel from 'svelte-carousel'  // ...</script>

SvelteKit support

There are several things to keep in mind whensvelte-carousel is used with SvelteKit. This is becausesvelte-carousel is a client-side library and depends ondocument andwindow.See more in SvelteKit FAQ.

  1. Installsvelte-carousel as a dev dependency.Why as a dev dependency?
yarn add svelte-carousel -D
npm install svelte-carousel -D
  1. Extendkit insvelte.config.js to include thevite property
constconfig={// existing propskit:{// existing propsvite:{optimizeDeps:{include:['lodash.get','lodash.isequal','lodash.clonedeep']}// plugins: []}}}
  1. Import and use it:
<script>  import Carousel from 'svelte-carousel';  import{browser} from '$app/environment';  let carousel; // for calling methods of the carousel instance    const handleNextClick = () =>{carousel.goToNext()}</script>{#ifbrowser}<Carouselbind:this={carousel}><div>1</div><div>2</div><div>3</div></Carousel>{/if}<buttonon:click={handleNextClick}>Next</button>

Vite support

  1. ExtendoptimizeDeps.include invite.config.js
exportdefaultdefineConfig({optimizeDeps:{include:['lodash.get','lodash.isequal','lodash.clonedeep']}})
  1. Import and use it:
<script>  import Carousel from 'svelte-carousel'  let carousel; // for calling methods of the carousel instance  const handleNextClick = () =>{carousel.goToNext()}</script><Carouselbind:this={carousel}><div>1</div><div>2</div><div>3</div></Carousel><buttonon:click={handleNextClick}>Next</button>

Props

PropTypeDefaultDescription
arrowsbooleantrueEnables next/prev arrows
infinitebooleantrueInfinite looping
initialPageIndexnumber0Page to start on
durationnumber500Transition duration (ms)
autoplaybooleanfalseEnables autoplay of pages
autoplayDurationnumber3000Autoplay change interval (ms)
autoplayDirectionstring'next'Autoplay change direction (next orprev)
pauseOnFocusbooleanfalsePauses autoplay on focus (for touchable devices - tap the carousel to toggle the autoplay, for non-touchable devices - hover over the carousel to pause the autoplay)
autoplayProgressVisiblebooleanfalseShows autoplay duration progress indicator
dotsbooleantrueCurrent page indicator dots
timingFunctionstring'ease-in-out'CSS animation timing function
swipingbooleantrueEnables swiping
particlesToShownumber1Number of elements to show
particlesToScrollnumber1Number of elements to scroll

Events

pageChange

It is dispatched on page change

Payload fieldTypeDescription
event.detailnumberCurrent page index
<Carouselon:pageChange={event=>console.log(`Current page index:${event.detail}`)}>  <!-- --></Carousel>

Slots

prev andnext

They are used for customizing prev and next buttons.

Slot props:

PropTypeDescription
showPrevPagefunctionCall it to switch to the previos page
showNextPagefunctionCall it to switch to the next page
<Carousellet:showPrevPagelet:showNextPage><divslot="prev">    <!-- --></div><divslot="next">    <!-- --></div>  <!-- --></Carousel>

dots

This slot is used for customizing how dots look like.

Slot props:

PropTypeDescription
currentPageIndexnumberRepresents current page index (start from 0)
pagesCountnumberTotal pages amount
showPagefunctionTakes index as page to be shown
<Carousellet:currentPageIndexlet:pagesCountlet:showPage><divslot="dots">    <!-- --></div>  <!-- --></Carousel>

Default slot

This slot takes content for the carousel.

Slot props:

PropTypeDescription
loadednumber[]Contains indexes of pages to be loaded. Can be used for lazy loading
currentPageIndexnumberRepresents current page index (start from 0)
<Carousellet:loaded><div>    <!-- --></div>  <!-- --></Carousel>

Methods

goTo

Navigates to a page by index.(pageIndex, options) => Promise<void>.

Arguments:

ArgumentTypeDefaultDescription
pageIndexnumberPage number
options.animatedbooleantrueShould it be animated or not
<script>  // ...  let carousel;  function goToStartPage(){carousel.goTo(0,{animated:false})}</script><Carouselbind:this={carousel}>  <!--  --></Carousel><buttonclass="button"on:click={goToStartPage}>Go</button>

goToPrev

Navigates to the previous page.(options) => Promise<void>.

Arguments:

ArgumentTypeDefaultDescription
options.animatedbooleantrueShould it be animated or not
<script>  // ...  let carousel;  function goToPrevPage(){carousel.goToPrev({animated:false})}</script><Carouselbind:this={carousel}>  <!--  --></Carousel><buttonclass="button"on:click={goToPrevPage}>Go</button>

goToNext

Navigates to the next page.(options) => Promise<void>.

Arguments:

ArgumentTypeDefaultDescription
options.animatedbooleantrueShould it be animated or not
<script>  // ...  let carousel;  function goToNextPage(){carousel.goToNext({animated:false})}</script><Carouselbind:this={carousel}>  <!--  --></Carousel><buttonclass="button"on:click={goToNextPage}>Go</button>

[8]ページ先頭

©2009-2025 Movatter.jp