slideToClickedSlide.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import {
  2. nextTick
  3. } from '../../shared/utils.js';
  4. export default function slideToClickedSlide() {
  5. const swiper = this;
  6. const {
  7. params,
  8. $wrapperEl
  9. } = swiper;
  10. const slidesPerView = params.slidesPerView === 'auto' ? swiper.slidesPerViewDynamic() : params.slidesPerView;
  11. let slideToIndex = swiper.clickedIndex;
  12. let realIndex;
  13. if (params.loop) {
  14. if (swiper.animating) return;
  15. // realIndex = parseInt($(swiper.clickedSlide).attr('data-swiper-slide-index'), 10);
  16. realIndex = parseInt(swiper.activeIndex, 10);
  17. if (params.centeredSlides) {
  18. if (slideToIndex < swiper.loopedSlides - slidesPerView / 2 || slideToIndex > swiper.slides.length - swiper
  19. .loopedSlides + slidesPerView / 2) {
  20. swiper.loopFix();
  21. slideToIndex = $wrapperEl.children(
  22. `.${params.slideClass}[data-swiper-slide-index="${realIndex}"]:not(.${params.slideDuplicateClass})`
  23. ).eq(0).index();
  24. nextTick(() => {
  25. swiper.slideTo(slideToIndex);
  26. });
  27. } else {
  28. swiper.slideTo(slideToIndex);
  29. }
  30. } else if (slideToIndex > swiper.slides.length - slidesPerView) {
  31. swiper.loopFix();
  32. slideToIndex = $wrapperEl.children(
  33. `.${params.slideClass}[data-swiper-slide-index="${realIndex}"]:not(.${params.slideDuplicateClass})`)
  34. .eq(0).index();
  35. nextTick(() => {
  36. swiper.slideTo(slideToIndex);
  37. });
  38. } else {
  39. swiper.slideTo(slideToIndex);
  40. }
  41. } else {
  42. swiper.slideTo(slideToIndex);
  43. }
  44. }