滑动滑块-带有slideToClickedSlide的缩略图库无法正常工作

时间:2018-09-18 19:20:36

标签: javascript swiper

用于拇指画廊工作的新的Swiper API(v 4.4.1)非常有用,仅是一个小功能-slideToClickedSlide。我希望图库在到达我的拇指视口中的最后一张幻灯片时会滑到下一张幻灯片。

 var galleryThumbs = new Swiper('.gallery-thumbs', {
      spaceBetween: 10,
      slidesPerView: 4,
      watchSlidesVisibility: true,
      watchSlidesProgress: true,
      centerInsufficientSlides: true,
      slideToClickedSlide: true
 });
 var galleryTop = new Swiper('.gallery-top', {
      spaceBetween: 10,
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      thumbs: {
        swiper: galleryThumbs
      }
 });

有人可以帮助我吗? 链接到postinstall

3 个答案:

答案 0 :(得分:1)

With plain JavaScript:

https://jsfiddle.net/sa7qwz25/171/

var galleryTop = new Swiper('.gallery-top', {
  ...
   on: {
    slideChange: function () {
      let activeIndex = this.activeIndex + 1;

      let activeSlide = document.querySelector(`.gallery-thumbs .swiper-slide:nth-child(${activeIndex})`);
      let nextSlide = document.querySelector(`.gallery-thumbs .swiper-slide:nth-child(${activeIndex + 1})`);
      let prevSlide = document.querySelector(`.gallery-thumbs .swiper-slide:nth-child(${activeIndex - 1})`);

      if (nextSlide && !nextSlide.classList.contains('swiper-slide-visible')) {
          this.thumbs.swiper.slideNext()    
      } else if (prevSlide && !prevSlide.classList.contains('swiper-slide-visible')) {
          this.thumbs.swiper.slidePrev()    
      }    
    }
  }
});

答案 1 :(得分:1)

如果需要使用多个Swiper实例:

var galleryTop = new Swiper('.gallery-top', {
  ...
   on: {
    slideChange: function () {
      let activeIndex = this.activeIndex + 1;
      let nextSlide = this.thumbs.swiper.$el[0].querySelector(`.swiper-slide:nth-child(${activeIndex + 1})`);
      let prevSlide = this.thumbs.swiper.$el[0].querySelector(`.swiper-slide:nth-child(${activeIndex - 1})`);

      if (nextSlide && !nextSlide.classList.contains('swiper-slide-visible')) {
          this.thumbs.swiper.slideNext()    
      } else if (prevSlide && !prevSlide.classList.contains('swiper-slide-visible')) {
          this.thumbs.swiper.slidePrev()    
      }    
    }
  }
});

答案 2 :(得分:1)

为此,我创建了一个函数,可以尝试,它更简单,并且您可以根据需要个性化div,请尝试:

//here you get your div(thumb) with data set
let swiperData = document.querySelectorAll('.myThumb');

//here i create a for to get my click and show my slider with my data
for(let i = 0, lgt = swiperData.length; i < lgt; i++) { 
    swiperData[i].onclick = function() { 
        var swiperGetData = swiperData[i].getAttribute('data-slide');
        mySwiper.slideTo(swiperGetData, 400);   

    }
}