Search code examples
javascriptjquerysliderswiper.js

Swiper: Set custom slide to active


I'm using Swiper to show a slider with every month of the year. It slides from left to right.

I now want to start with the current month on the left side. If there are month before, they should be in the left offset.

I tried to give the .swiper-slide class the active class .swiper-slide-active:

<div class="swiper-slide swiper-slide-active">

But that doesnt work.

In the docs I found no parameter to set a acive slide in the settings. Is there any way to do this?

Here's my current code: https://codepen.io/cray_code/pen/dydrLBP

var swiper = new Swiper(".swiper-months", {
    cssMode: false,
    slidesPerView: 3.5,
    spaceBetween: 16,
    activeIndex: 6,
    mousewheel: {
        forceToAxis: true,
    },
});

The activeIndex: 6 doesn't work either. I'm not sure if that's correct.


Solution

  • Try initialSlide, activeIndex is just for getting the actual active index. But I think you shoud use 5 insted of 6 to start on sixth month, because first month starts with 1 and indexing starts from 0.

    var swiper = new Swiper(".swiper-months", {
        initialSlide: 6,
        cssMode: false,
        slidesPerView: 3.5,
        spaceBetween: 16,
        mousewheel: {
            forceToAxis: true,
        },
    });