infinity-forge
Version:
160 lines • 6.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = slideTo;
var utils_1 = require("../../shared/utils.js");
function slideTo(index, speed, runCallbacks, internal, initial) {
var _a;
if (index === void 0) { index = 0; }
if (runCallbacks === void 0) { runCallbacks = true; }
if (typeof index === 'string') {
index = parseInt(index, 10);
}
var swiper = this;
var slideIndex = index;
if (slideIndex < 0)
slideIndex = 0;
var params = swiper.params, snapGrid = swiper.snapGrid, slidesGrid = swiper.slidesGrid, previousIndex = swiper.previousIndex, activeIndex = swiper.activeIndex, rtl = swiper.rtlTranslate, wrapperEl = swiper.wrapperEl, enabled = swiper.enabled;
if ((!enabled && !internal && !initial) ||
swiper.destroyed ||
(swiper.animating && params.preventInteractionOnTransition)) {
return false;
}
if (typeof speed === 'undefined') {
speed = swiper.params.speed;
}
var skip = Math.min(swiper.params.slidesPerGroupSkip, slideIndex);
var snapIndex = skip + Math.floor((slideIndex - skip) / swiper.params.slidesPerGroup);
if (snapIndex >= snapGrid.length)
snapIndex = snapGrid.length - 1;
var translate = -snapGrid[snapIndex];
// Normalize slideIndex
if (params.normalizeSlideIndex) {
for (var i = 0; i < slidesGrid.length; i += 1) {
var normalizedTranslate = -Math.floor(translate * 100);
var normalizedGrid = Math.floor(slidesGrid[i] * 100);
var normalizedGridNext = Math.floor(slidesGrid[i + 1] * 100);
if (typeof slidesGrid[i + 1] !== 'undefined') {
if (normalizedTranslate >= normalizedGrid &&
normalizedTranslate < normalizedGridNext - (normalizedGridNext - normalizedGrid) / 2) {
slideIndex = i;
}
else if (normalizedTranslate >= normalizedGrid &&
normalizedTranslate < normalizedGridNext) {
slideIndex = i + 1;
}
}
else if (normalizedTranslate >= normalizedGrid) {
slideIndex = i;
}
}
}
// Directions locks
if (swiper.initialized && slideIndex !== activeIndex) {
if (!swiper.allowSlideNext &&
(rtl
? translate > swiper.translate && translate > swiper.minTranslate()
: translate < swiper.translate && translate < swiper.minTranslate())) {
return false;
}
if (!swiper.allowSlidePrev &&
translate > swiper.translate &&
translate > swiper.maxTranslate()) {
if ((activeIndex || 0) !== slideIndex) {
return false;
}
}
}
if (slideIndex !== (previousIndex || 0) && runCallbacks) {
swiper.emit('beforeSlideChangeStart');
}
// Update progress
swiper.updateProgress(translate);
var direction;
if (slideIndex > activeIndex)
direction = 'next';
else if (slideIndex < activeIndex)
direction = 'prev';
else
direction = 'reset';
// Update Index
if ((rtl && -translate === swiper.translate) || (!rtl && translate === swiper.translate)) {
swiper.updateActiveIndex(slideIndex);
// Update Height
if (params.autoHeight) {
swiper.updateAutoHeight();
}
swiper.updateSlidesClasses();
if (params.effect !== 'slide') {
swiper.setTranslate(translate);
}
if (direction !== 'reset') {
swiper.transitionStart(runCallbacks, direction);
swiper.transitionEnd(runCallbacks, direction);
}
return false;
}
if (params.cssMode) {
var isH_1 = swiper.isHorizontal();
var t_1 = rtl ? translate : -translate;
if (speed === 0) {
var isVirtual = swiper.virtual && swiper.params.virtual.enabled;
if (isVirtual) {
swiper.wrapperEl.style.scrollSnapType = 'none';
swiper._immediateVirtual = true;
}
if (isVirtual && !swiper._cssModeVirtualInitialSet && swiper.params.initialSlide > 0) {
swiper._cssModeVirtualInitialSet = true;
requestAnimationFrame(function () {
wrapperEl[isH_1 ? 'scrollLeft' : 'scrollTop'] = t_1;
});
}
else {
wrapperEl[isH_1 ? 'scrollLeft' : 'scrollTop'] = t_1;
}
if (isVirtual) {
requestAnimationFrame(function () {
swiper.wrapperEl.style.scrollSnapType = '';
swiper._immediateVirtual = false;
});
}
}
else {
if (!swiper.support.smoothScroll) {
(0, utils_1.animateCSSModeScroll)({ swiper: swiper, targetPosition: t_1, side: isH_1 ? 'left' : 'top' });
return true;
}
wrapperEl.scrollTo((_a = {},
_a[isH_1 ? 'left' : 'top'] = t_1,
_a.behavior = 'smooth',
_a));
}
return true;
}
swiper.setTransition(speed);
swiper.setTranslate(translate);
swiper.updateActiveIndex(slideIndex);
swiper.updateSlidesClasses();
swiper.emit('beforeTransitionStart', speed, internal);
swiper.transitionStart(runCallbacks, direction);
if (speed === 0) {
swiper.transitionEnd(runCallbacks, direction);
}
else if (!swiper.animating) {
swiper.animating = true;
if (!swiper.onSlideToWrapperTransitionEnd) {
swiper.onSlideToWrapperTransitionEnd = function transitionEnd(e) {
if (!swiper || swiper.destroyed)
return;
if (e.target !== this)
return;
swiper.wrapperEl.removeEventListener('transitionend', swiper.onSlideToWrapperTransitionEnd);
swiper.onSlideToWrapperTransitionEnd = null;
delete swiper.onSlideToWrapperTransitionEnd;
swiper.transitionEnd(runCallbacks, direction);
};
}
swiper.wrapperEl.addEventListener('transitionend', swiper.onSlideToWrapperTransitionEnd);
}
return true;
}
//# sourceMappingURL=slideTo.js.map