Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to a specific slide with iDangerous Swiper

Tags:

jquery

swiper

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() );
});
like image 414
user2654675 Avatar asked May 28 '14 21:05

user2654675


People also ask

How do you destroy swiper on min width breakpoints?

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.

What is Swiperjs?

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.

How do I get rid of dots in swiper js?

Remove this pagination={{clickable: true,}} from your code (That's it).


2 Answers

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 );
});
like image 135
cor Avatar answered Nov 06 '22 23:11

cor


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
});
like image 28
DrewB Avatar answered Nov 06 '22 23:11

DrewB