UNPKG

1.5 kBJavaScriptView Raw
1export default function addSlide(index, slides) {
2 const swiper = this;
3 const {
4 $wrapperEl,
5 params,
6 activeIndex
7 } = swiper;
8 let activeIndexBuffer = activeIndex;
9
10 if (params.loop) {
11 activeIndexBuffer -= swiper.loopedSlides;
12 swiper.loopDestroy();
13 swiper.slides = $wrapperEl.children(`.${params.slideClass}`);
14 }
15
16 const baseLength = swiper.slides.length;
17
18 if (index <= 0) {
19 swiper.prependSlide(slides);
20 return;
21 }
22
23 if (index >= baseLength) {
24 swiper.appendSlide(slides);
25 return;
26 }
27
28 let newActiveIndex = activeIndexBuffer > index ? activeIndexBuffer + 1 : activeIndexBuffer;
29 const slidesBuffer = [];
30
31 for (let i = baseLength - 1; i >= index; i -= 1) {
32 const currentSlide = swiper.slides.eq(i);
33 currentSlide.remove();
34 slidesBuffer.unshift(currentSlide);
35 }
36
37 if (typeof slides === 'object' && 'length' in slides) {
38 for (let i = 0; i < slides.length; i += 1) {
39 if (slides[i]) $wrapperEl.append(slides[i]);
40 }
41
42 newActiveIndex = activeIndexBuffer > index ? activeIndexBuffer + slides.length : activeIndexBuffer;
43 } else {
44 $wrapperEl.append(slides);
45 }
46
47 for (let i = 0; i < slidesBuffer.length; i += 1) {
48 $wrapperEl.append(slidesBuffer[i]);
49 }
50
51 if (params.loop) {
52 swiper.loopCreate();
53 }
54
55 if (!params.observer) {
56 swiper.update();
57 }
58
59 if (params.loop) {
60 swiper.slideTo(newActiveIndex + swiper.loopedSlides, 0, false);
61 } else {
62 swiper.slideTo(newActiveIndex, 0, false);
63 }
64}
\No newline at end of file