最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change centered slide by clicking on desired slide in swiper.js? - Stack Overflow

programmeradmin1浏览0评论

I use swiper in centeredSlides mode and loop option is true. I want to when I click on a slide, that slide set as centered slide in swiper carousel. Swiper give me clicked slide index, but how to use it for change the centered slide?

These are my options:

  slidesPerView: 4.5,
  spaceBetween: 20,
  updateOnWindowResize: true,
  loop: true,
  grabCursor: true,
  centeredSlides: true,
  centeredSlidesBounds: true,
  initialSlide: 0,
  on: {
    click() {
      console.log(this.clickedIndex);
    },
  },

I use swiper in centeredSlides mode and loop option is true. I want to when I click on a slide, that slide set as centered slide in swiper carousel. Swiper give me clicked slide index, but how to use it for change the centered slide?

These are my options:

  slidesPerView: 4.5,
  spaceBetween: 20,
  updateOnWindowResize: true,
  loop: true,
  grabCursor: true,
  centeredSlides: true,
  centeredSlidesBounds: true,
  initialSlide: 0,
  on: {
    click() {
      console.log(this.clickedIndex);
    },
  },
Share Improve this question asked Nov 17, 2020 at 7:25 Heyran.rsHeyran.rs 5332 gold badges11 silver badges25 bronze badges 1
  • 1 Are you able to create a fiddle or working example of this, with the HTML and CSS? Otherwise any solution may not look like what you're seeing. – sbgib Commented Nov 28, 2020 at 10:48
Add a ment  | 

2 Answers 2

Reset to default 5 +50

Like this:

var mySwiper = new Swiper('.swiper-container', {
  slidesPerView: 4.5,
  spaceBetween: 20,
  updateOnWindowResize: true,
  loop: true,
  grabCursor: true,
  centeredSlides: true,
  centeredSlidesBounds: true,
  initialSlide: 0,
  on: {
    click() {
        console.log('index', this.clickedIndex);
        mySwiper.slideTo(this.clickedIndex);    
    },
  },
});

Codepen: https://codepen.io/Terrafire123/pen/gOwbjMy

Just set slideToClickedSlide to true and click on any slide will produce transition to this slide

发布评论

评论列表(0)

  1. 暂无评论