Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Swiper JS method in Svelte

I want to use slideNext() method in Swiper JS. However, from the documentation of Swiper JS for Svelte, there is no example on showing how to access to method in Svelte. In React, there is a hook called useSwiper but no such thing in Svelte. Does anyone know how?

like image 244
Hon Wi Cong Avatar asked Oct 29 '25 09:10

Hon Wi Cong


1 Answers

The API is a bit weird. It should be using a bindable prop but you need to handle the swiper event instead to get the API instance.

<script>
  // ...
  let swiper;
</script>

<Swiper on:swiper={e => swiper = e.detail[0]}>
    <SwiperSlide>Slide 1</SwiperSlide>
    <SwiperSlide>Slide 2</SwiperSlide>
    <SwiperSlide>Slide 3</SwiperSlide>
</Swiper>

<button type=button on:click={() => swiper.slidePrev()}>Previous</button>
<button type=button on:click={() => swiper.slideNext()}>Next</button>

REPL example

like image 192
H.B. Avatar answered Oct 31 '25 04:10

H.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!