Permalink
Browse files

addIndex first commit

  • Loading branch information...
kochizufan committed May 30, 2018
1 parent 066ab58 commit 4bc3192eab61cd78461b1309832ea7ee4c5356a4
Showing with 50 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +47 −0 src/components/core/manipulation/addSlide.js
  3. +2 −0 src/components/core/manipulation/index.js
@@ -5,3 +5,4 @@ custom
*.log
.versions
.idea
@@ -0,0 +1,47 @@
import Support from '../../../utils/support';
export default function (index, slides) {
const swiper = this;
const { $wrapperEl, params, activeIndex } = swiper;
if (params.loop) {
swiper.loopDestroy();
swiper.slides = $wrapperEl.children(`.${params.slideClass}`);
}
const baseLength = swiper.slides.length;
if (index <= 0) {
swiper.prependSlide(slides);
return;
} else if (index >= baseLength) {
swiper.appendSlide(slides);
return;
}
let newActiveIndex = activeIndex > index ? activeIndex + 1 : activeIndex;
const slidesBuffer = [];
for (let i = baseLength - 1; i >= index; i -= 1) {
const currentSlide = swiper.slides.eq(i);
currentSlide.remove();
slidesBuffer.unshift(currentSlide);
}
if (typeof slides === 'object' && 'length' in slides) {
for (let i = 0; i < slides.length; i += 1) {
if (slides[i]) $wrapperEl.append(slides[i]);
}
newActiveIndex = activeIndex > index ? activeIndex + slides.length : activeIndex;
} else {
$wrapperEl.append(slides);
}
for (let i = 0; i < slidesBuffer.length; i += 1) {
$wrapperEl.append(slidesBuffer[i]);
}
if (params.loop) {
swiper.loopCreate();
}
if (!(params.observer && Support.observer)) {
swiper.update();
}
swiper.slideTo(newActiveIndex, 0, false);
}
@@ -1,11 +1,13 @@
import appendSlide from './appendSlide';
import prependSlide from './prependSlide';
import addSlide from './addSlide';
import removeSlide from './removeSlide';
import removeAllSlides from './removeAllSlides';
export default {
appendSlide,
prependSlide,
addSlide,
removeSlide,
removeAllSlides,
};

0 comments on commit 4bc3192

Please sign in to comment.