If I have a custom menu.
How can I go to a specific slide at any moment in Swiper.js?
<div class="menu">
<ul>
<li class="slide-3">go to slide 3</li>
<li class="slide-5">go to slide 5</li>
<li class="slide-1">go to slide 1</li>
</ul>
</div>
I tried something like this but is not working:
$('.slide-3').click(function(e) {
e.preventDefault();
$(".menu .active").removeClass('active');
$(this).addClass('active');
swipeTo( $('.pag2').index() );
});
Combining the destroy parameter with Window. matchMedia() is an effective way to manage Swiper on different screen sizes. Mobile first is optional—max-width, e.g., window. matchMedia( '(max-width:31.25em)' ), will work just as well.
Swiper is a JavaScript library that creates modern touch sliders with hardware-accelerated transitions (utilizing GPU to offload graphic-intensive transitions and create smoother visuals) and excellent native behavior. Swiper is available for vanilla JavaScript, Angular, React, Vue. js, and Svelte.
Remove this pagination={{clickable: true,}} from your code (That's it).
From the documentation page:
mySwiper.slideTo(index, speed, runCallbacks);
Run transition to the slide with index number equal to 'index' parameter for the speed equal to 'speed' parameter. You can set 'runCallbacks' to false (by default it is 'true') and transition will not produce onSlideChange callback functions.
So in your case, you first need to declare a variable for example:
var mySwiper = new Swiper('.swiper-container',{
mode:'horizontal',
loop: false
});
And then:
$('.slide-3').click(function(e) {
e.preventDefault();
$(".menu .active").removeClass('active');
$(this).addClass('active');
mySwiper.slideTo( $('.pag2').index(),1000,false );
});
To build on Ahmed's answer, to implement:
var moveToClickedNumber = function(swiper){swiper.swipeTo(swiper.clickedSlideIndex)}
var mySwiper = new Swiper('.swiper-container',{
mode:'horizontal',
loop: false,
onSlideClick: moveToClickedNumber,
initialSlide: 0, //slide number which you want to show-- 0 by default
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With