slideToClosest.js 1.1 KB

12345678910111213141516171819202122232425262728
  1. /* eslint no-unused-vars: "off" */
  2. export default function slideToClosest(speed = this.params.speed, runCallbacks = true, internal, threshold = 0.5) {
  3. const swiper = this;
  4. let index = swiper.activeIndex;
  5. const skip = Math.min(swiper.params.slidesPerGroupSkip, index);
  6. const snapIndex = skip + Math.floor((index - skip) / swiper.params.slidesPerGroup);
  7. const translate = swiper.rtlTranslate ? swiper.translate : -swiper.translate;
  8. if (translate >= swiper.snapGrid[snapIndex]) {
  9. const currentSnap = swiper.snapGrid[snapIndex];
  10. const nextSnap = swiper.snapGrid[snapIndex + 1];
  11. if (translate - currentSnap > (nextSnap - currentSnap) * threshold) {
  12. index += swiper.params.slidesPerGroup;
  13. }
  14. } else {
  15. const prevSnap = swiper.snapGrid[snapIndex - 1];
  16. const currentSnap = swiper.snapGrid[snapIndex];
  17. if (translate - prevSnap <= (currentSnap - prevSnap) * threshold) {
  18. index -= swiper.params.slidesPerGroup;
  19. }
  20. }
  21. index = Math.max(index, 0);
  22. index = Math.min(index, swiper.slidesGrid.length - 1);
  23. return swiper.slideTo(index, speed, runCallbacks, internal);
  24. }