Solution: Last slide not getting active when 'slidePerView' set to 'auto'.

See original GitHub issue

I currently experienced the same problems like as many people did and also looked through the issues but no results. https://github.com/nolimits4web/swiper/issues/2571, https://github.com/nolimits4web/swiper/issues/1257, https://github.com/nolimits4web/swiper/issues/534 Finally, I found out a way to do it, a better way to do it guys!.

By setting the snapGrid settings as same as the slidesGrid settings after the swiper initialized. And then the swiper will work as what you expected.

const swiper = new Swiper(
    '.awesome-swiper', 
    {
        init: false,
        direction: 'horizontal',
        speed: 700,
        slidesPerView: 'auto',
        spaceBetween: 80,
    }
);

swiper.init();
swiper.snapGrid[swiper.snapGrid.length - 1] = swiper.slidesGrid[swiper.slidesGrid.length - 1];

or even completely replacing it by cloning the settings.

// can do this way
swiper.snapGrid = swiper.slidesGrid.slice(0);

// or ES6 way
swiper.snapGrid = [...swiper.slidesGrid];

Update: 2020-11-18

You will need to update the snapGrid when responsive by using built-in Events.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:13

github_iconTop GitHub Comments

14reactions
kubiqskcommented, Jul 19, 2021
on: {
	snapGridLengthChange:function(){
		if( this.snapGrid.length != this.slidesGrid.length ){
			this.snapGrid = this.slidesGrid.slice(0);
		}
	}
}
13reactions
WilliBautistacommented, Nov 17, 2020

I currently experienced the same problems like as many people did and also looked through the issues but no results. #2571, #1257, #534 Finally, I found out a way to do it, a better way to do it guys!.

By setting the snapGrid settings as same as the slidesGrid settings after the swiper initialized. And then the swiper will work as what you expected.

const swiper = new Swiper(
    '.awesome-swiper', 
    {
        init: false,
        direction: 'horizontal',
        speed: 700,
        slidesPerView: 'auto',
        spaceBetween: 80,
    }
);

swiper.init();
swiper.snapGrid[swiper.snapGrid.length - 1] = swiper.slidesGrid[swiper.slidesGrid.length - 1];

or even completely replacing it by cloning the settings.

// can do this way
swiper.snapGrid = swiper.slidesGrid.slice(0);

// or ES6 way
swiper.snapGrid = [...swiper.slidesGrid];

this work for me, but with a little adjustment: I placed this line this.snapGrid = [...this.slidesGrid]; inside of the reachEnd swiper event, like this:

const swiper = new Swiper('.awesome-swiper', {
    direction: 'horizontal',
    speed: 700,
    slidesPerView: 'auto',
    spaceBetween: 80,
    on: {
      reachEnd: function() {
        this.snapGrid = [...this.slidesGrid];
      },
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Swiper JS Slides not Sliding all the way the left - Stack Overflow
I have tried setting loop:true but this centres my first slide and displays a partial at the left that I don't want at...
Read more >
SwiperOptions | Swiper - v8.4.5
If true , then active slide will be centered without adding gaps at the beginning and end of slider. Required centeredSlides: true ....
Read more >
Last slide never becomes active when using Auto Slides Per ...
When the swiper is set to "slidesPerView: 'auto'" and the slides have different widths, the last slide doesn't seem to get as the...
Read more >
Set slidesPerView value with decimals? Or maybe override it ...
Hi, REALLY thank you for your quick answer! Well, I've tried to set “Centered Slides” option on and now my active slide is...
Read more >
Using .last() to check if we're on the last slide - Codecademy
I couldn't get your code to work (could be me) but I did succeed with this: ... Polepeddi made this not auto-dropdown, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found