loopCreate.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. export default function loopCreate() {
  2. const swiper = this;
  3. const {
  4. params,
  5. $wrapperEl,
  6. native
  7. } = swiper; // Remove duplicated slides
  8. const $selector = $wrapperEl;
  9. let slides = native.children;
  10. if (params.loopFillGroupWithBlank) {
  11. const blankSlidesNum = params.slidesPerGroup - slides.length % params.slidesPerGroup;
  12. if (blankSlidesNum !== params.slidesPerGroup) {
  13. native.loopBlankShow = true;
  14. native.loopBlankNumber = blankSlidesNum;
  15. }
  16. }
  17. if (params.slidesPerView === 'auto' && !params.loopedSlides) params.loopedSlides = slides.length;
  18. swiper.loopedSlides = Math.ceil(parseFloat(params.loopedSlides || params.slidesPerView, 10));
  19. swiper.loopedSlides += params.loopAdditionalSlides;
  20. if (swiper.loopedSlides > slides.length) {
  21. swiper.loopedSlides = slides.length;
  22. }
  23. const prependSlides = [];
  24. const appendSlides = [];
  25. slides.forEach((el, index) => {
  26. const slide = el;
  27. if (index < slides.length && index >= slides.length - swiper.loopedSlides) {
  28. prependSlides.push(el);
  29. }
  30. if (index < swiper.loopedSlides) {
  31. appendSlides.push(el);
  32. }
  33. });
  34. let list = [...swiper.native.value];
  35. let newList = [...list];
  36. swiper.originalDataList = [...swiper.native.value];
  37. for (let i = 0; i < appendSlides.length; i += 1) {
  38. newList.push(list[appendSlides[i].index]);
  39. }
  40. for (let i = prependSlides.length - 1; i >= 0; i -= 1) {
  41. newList.unshift(list[prependSlides[i].index]);
  42. }
  43. swiper.native.$emit("input", newList)
  44. return true;
  45. }