updateSlidesClasses.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. export default function updateSlidesClasses() {
  2. const swiper = this;
  3. const {
  4. slides,
  5. params,
  6. $wrapperEl,
  7. activeIndex,
  8. realIndex
  9. } = swiper;
  10. if (!slides.length || !$wrapperEl) return;
  11. const isVirtual = swiper.virtual && params.virtual.enabled;
  12. for (var i = 0; i < slides.length; i++) {
  13. slides[i].removeClass(
  14. `${params.slideActiveClass} ${params.slideNextClass} ${params.slidePrevClass} ${params.slideDuplicateActiveClass} ${params.slideDuplicateNextClass} ${params.slideDuplicatePrevClass}`
  15. );
  16. }
  17. let activeSlide;
  18. if (isVirtual) {
  19. // activeSlide = swiper.$wrapperEl.find(`.${params.slideClass}[data-swiper-slide-index="${activeIndex}"]`);
  20. activeSlide = slides[slides.findIndex((item) => {
  21. return item.dataSwiperSlideIndex == activeIndex
  22. })];
  23. } else {
  24. activeSlide = slides[activeIndex];
  25. } // Active classes
  26. if (!activeSlide) return
  27. activeSlide.addClass(params.slideActiveClass);
  28. if (params.loop) {
  29. if (activeSlide.hasClass(params.slideDuplicateClass)) {
  30. // $wrapperEl.children[realIndex].addClass(params.slideDuplicateActiveClass);
  31. let index = slides.findIndex((item) => {
  32. return !item.hasClass(params.slideDuplicateClass) && item.dataSwiperSlideIndex == realIndex
  33. })
  34. slides[index] && slides[index].addClass(params.slideDuplicateActiveClass);
  35. } else {
  36. // $wrapperEl.children[realIndex].addClass(params.slideDuplicateActiveClass);
  37. let index = slides.findIndex((item) => {
  38. return item.hasClass(params.slideDuplicateClass) && item.dataSwiperSlideIndex == realIndex
  39. })
  40. slides[index] && slides[index].addClass(params.slideDuplicateActiveClass);
  41. }
  42. } // Next Slide
  43. let nextSlide = activeSlide.nextAll(`.${params.slideClass}`)[0];
  44. if (nextSlide) {
  45. nextSlide.addClass(params.slideNextClass);
  46. } else {
  47. if (params.loop && !nextSlide) {
  48. nextSlide = slides[0];
  49. nextSlide.addClass(params.slideNextClass);
  50. } // Prev Slide
  51. }
  52. let prevSlide = activeSlide.prevAll(`.${params.slideClass}`)[0];
  53. if (prevSlide) {
  54. prevSlide.addClass(params.slidePrevClass);
  55. } else {
  56. if (params.loop && !prevSlide) {
  57. prevSlide = slides[slides.length - 1];
  58. prevSlide.addClass(params.slidePrevClass);
  59. }
  60. }
  61. if (params.loop) {
  62. // Duplicate to all looped slides
  63. if (nextSlide.hasClass(params.slideDuplicateClass)) {
  64. // $wrapperEl.children(
  65. // nextSlide.dataSwiperSlideIndex
  66. // ).addClass(params.slideDuplicateNextClass);
  67. let index = slides.findIndex((item) => {
  68. return !item.hasClass(params.slideDuplicateClass) && item.dataSwiperSlideIndex == nextSlide
  69. .dataSwiperSlideIndex
  70. })
  71. slides[index] && slides[index].addClass(params.slideDuplicateNextClass);
  72. } else {
  73. // $wrapperEl.children(
  74. // nextSlide.dataSwiperSlideIndex
  75. // ).addClass(params.slideDuplicateNextClass);
  76. let index = slides.findIndex((item) => {
  77. return item.hasClass(params.slideDuplicateClass) && item.dataSwiperSlideIndex == nextSlide
  78. .dataSwiperSlideIndex
  79. })
  80. slides[index] && slides[index].addClass(params.slideDuplicateNextClass);
  81. }
  82. if (prevSlide.hasClass(params.slideDuplicateClass)) {
  83. // $wrapperEl.children(
  84. // prevSlide.dataSwiperSlideIndex
  85. // ).addClass(params.slideDuplicatePrevClass);
  86. let index = slides.findIndex((item) => {
  87. return !item.hasClass(params.slideDuplicateClass) && item.dataSwiperSlideIndex == prevSlide
  88. .dataSwiperSlideIndex
  89. })
  90. slides[index] && slides[index].addClass(params.slideDuplicatePrevClass);
  91. } else {
  92. // $wrapperEl.children(
  93. // prevSlide.dataSwiperSlideIndex
  94. // ).addClass(params.slideDuplicatePrevClass);
  95. let index = slides.findIndex((item) => {
  96. return item.hasClass(params.slideDuplicateClass) && item.dataSwiperSlideIndex == prevSlide
  97. .dataSwiperSlideIndex
  98. })
  99. slides[index] && slides[index].addClass(params.slideDuplicatePrevClass);
  100. }
  101. }
  102. swiper.emitSlidesClasses();
  103. }