Skip to content

Commit

Permalink
Ability to change direction
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Feb 22, 2019
1 parent 973cc8b commit e487d5e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/components/core/breakpoints/setBreakpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export default function () {
}

const breakpointParams = breakpointOnlyParams || swiper.originalParams;
const needsReLoop = params.loop && (breakpointParams.slidesPerView !== params.slidesPerView);
const directionChanged = breakpointParams.direction && breakpointParams.direction !== params.direction;
const needsReLoop = params.loop && (breakpointParams.slidesPerView !== params.slidesPerView || directionChanged);

if (directionChanged && initialized) {
swiper.changeDirection();
}

Utils.extend(swiper.params, breakpointParams);

Expand All @@ -46,6 +51,7 @@ export default function () {
swiper.updateSlides();
swiper.slideTo((activeIndex - loopedSlides) + swiper.loopedSlides, 0, false);
}

swiper.emit('breakpoint', breakpointParams);
}
}
48 changes: 48 additions & 0 deletions src/components/core/core-class.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint no-param-reassign: "off" */
import $ from '../../utils/dom';
import Utils from '../../utils/utils';
import Support from '../../utils/support';
import Browser from '../../utils/browser';

import SwiperClass from '../../utils/class';

Expand Down Expand Up @@ -316,6 +318,52 @@ class Swiper extends SwiperClass {
swiper.emit('update');
}

changeDirection(newDirection, needUpdate = true) {
const swiper = this;
const currentDirection = swiper.params.direction;
if (!newDirection) {
// eslint-disable-next-line
newDirection = currentDirection === 'horizontal' ? 'vertical' : 'horizontal';
}
if ((newDirection === currentDirection) || (newDirection !== 'horizontal' && newDirection !== 'vertical')) {
return swiper;
}

if (currentDirection === 'vertical') {
swiper.$el
.removeClass(`${swiper.params.containerModifierClass}vertical wp8-vertical`)
.addClass(`${swiper.params.containerModifierClass}${newDirection}`);

if ((Browser.isIE || Browser.isEdge) && (Support.pointerEvents || Support.prefixedPointerEvents)) {
swiper.$el.addClass(`${swiper.params.containerModifierClass}wp8-${newDirection}`);
}
}
if (currentDirection === 'horizontal') {
swiper.$el
.removeClass(`${swiper.params.containerModifierClass}horizontal wp8-horizontal`)
.addClass(`${swiper.params.containerModifierClass}${newDirection}`);

if ((Browser.isIE || Browser.isEdge) && (Support.pointerEvents || Support.prefixedPointerEvents)) {
swiper.$el.addClass(`${swiper.params.containerModifierClass}wp8-${newDirection}`);
}
}

swiper.params.direction = newDirection;

swiper.slides.each((slideIndex, slideEl) => {
if (newDirection === 'vertical') {
slideEl.style.width = '';
} else {
slideEl.style.height = '';
}
});

swiper.emit('changeDirection');
if (needUpdate) swiper.update();

return swiper;
}

init() {
const swiper = this;
if (swiper.initialized) return;
Expand Down

0 comments on commit e487d5e

Please sign in to comment.