updateActiveIndex.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. export default function updateActiveIndex(newActiveIndex) {
  2. const swiper = this;
  3. const translate = swiper.rtlTranslate ? swiper.translate : -swiper.translate;
  4. const {
  5. slidesGrid,
  6. snapGrid,
  7. params,
  8. activeIndex: previousIndex,
  9. realIndex: previousRealIndex,
  10. snapIndex: previousSnapIndex
  11. } = swiper;
  12. let activeIndex = newActiveIndex;
  13. let snapIndex;
  14. if (typeof activeIndex === 'undefined') {
  15. for (let i = 0; i < slidesGrid.length; i += 1) {
  16. if (typeof slidesGrid[i + 1] !== 'undefined') {
  17. if (translate >= slidesGrid[i] && translate < slidesGrid[i + 1] - (slidesGrid[i + 1] - slidesGrid[i]) /
  18. 2) {
  19. activeIndex = i;
  20. } else if (translate >= slidesGrid[i] && translate < slidesGrid[i + 1]) {
  21. activeIndex = i + 1;
  22. }
  23. } else if (translate >= slidesGrid[i]) {
  24. activeIndex = i;
  25. }
  26. } // Normalize slideIndex
  27. if (params.normalizeSlideIndex) {
  28. if (activeIndex < 0 || typeof activeIndex === 'undefined') activeIndex = 0;
  29. }
  30. }
  31. if (snapGrid.indexOf(translate) >= 0) {
  32. snapIndex = snapGrid.indexOf(translate);
  33. } else {
  34. const skip = Math.min(params.slidesPerGroupSkip, activeIndex);
  35. snapIndex = skip + Math.floor((activeIndex - skip) / params.slidesPerGroup);
  36. }
  37. if (snapIndex >= snapGrid.length) snapIndex = snapGrid.length - 1;
  38. if (swiper.loopedSlides) {
  39. swiper.slides.filter((item) => item.index >= swiper.loopedSlides && item.index < swiper.slides.length - swiper
  40. .loopedSlides).forEach((item, index) => {
  41. item.dataSwiperSlideIndex = item.index - swiper.loopedSlides;
  42. })
  43. swiper.slides.filter((item) => item.index < swiper.loopedSlides).forEach((item, index) => {
  44. if (swiper.slides[swiper.slides.length - swiper.loopedSlides * 3 + index]) {
  45. item.dataSwiperSlideIndex = swiper.slides[swiper.slides.length - swiper.loopedSlides * 3 +
  46. index]
  47. .index;
  48. }
  49. })
  50. swiper.slides.filter((item) => item.index >= swiper.slides.length - swiper
  51. .loopedSlides).forEach((item, index) => {
  52. item.dataSwiperSlideIndex = swiper.slides[index].index;
  53. })
  54. }
  55. if (activeIndex === previousIndex) {
  56. if (snapIndex !== previousSnapIndex) {
  57. swiper.snapIndex = snapIndex;
  58. swiper.emit('snapIndexChange');
  59. }
  60. return;
  61. } // Get real index
  62. let realIndex;
  63. if (swiper.virtual && params.virtual.enabled) {
  64. realIndex = activeIndex;
  65. } else {
  66. if (swiper.slides[activeIndex].dataSwiperSlideIndex == undefined || swiper.slides[activeIndex]
  67. .dataSwiperSlideIndex == null) {
  68. realIndex = activeIndex;
  69. } else {
  70. realIndex = swiper.slides[activeIndex].dataSwiperSlideIndex;
  71. }
  72. }
  73. Object.assign(swiper, {
  74. snapIndex,
  75. realIndex,
  76. previousIndex,
  77. activeIndex
  78. });
  79. swiper.emit('activeIndexChange');
  80. swiper.emit('snapIndexChange');
  81. if (previousRealIndex !== realIndex) {
  82. swiper.emit('realIndexChange');
  83. }
  84. if (swiper.initialized || swiper.params.runCallbacksOnInit) {
  85. swiper.emit('slideChange', activeIndex);
  86. }
  87. }