Skip to content

Commit

Permalink
feat(cards-effect): add slide shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Aug 11, 2021
1 parent 0d2a349 commit 09666c8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/core/core.less
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@
}
.swiper-wrapper,
.swiper-slide,
.swiper-slide-shadow,
.swiper-slide-shadow-left,
.swiper-slide-shadow-right,
.swiper-slide-shadow-top,
.swiper-slide-shadow-bottom,
.swiper-cube-shadow {
transform-style: preserve-3d;
}
.swiper-slide-shadow,
.swiper-slide-shadow-left,
.swiper-slide-shadow-right,
.swiper-slide-shadow-top,
Expand All @@ -81,6 +83,9 @@
pointer-events: none;
z-index: 10;
}
.swiper-slide-shadow {
background: rgba(0, 0, 0, 0.1);
}
.swiper-slide-shadow-left {
background-image: linear-gradient(to left, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0));
}
Expand Down
15 changes: 14 additions & 1 deletion src/modules/effect-cards/effect-cards.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import createShadow from '../../shared/create-shadow.js';
import virtualEffectTransitionEnd from '../../shared/virtual-effect-transition-end.js';

export default function EffectCards({ swiper, extendParams, on }) {
extendParams({
cardsEffect: {
slideShadows: true,
transformEl: null,
},
});
Expand Down Expand Up @@ -74,11 +76,22 @@ export default function EffectCards({ swiper, extendParams, on }) {
scale(${scaleString})
`;

if (params.slideShadows) {
// Set shadows
let $shadowEl = $slideEl.find('.swiper-slide-shadow');
if ($shadowEl.length === 0) {
$shadowEl = createShadow(params, $slideEl);
}
if ($shadowEl.length)
$shadowEl[0].style.opacity = Math.min(Math.max(Math.abs(progress), 0), 1);
}

$slideEl[0].style.zIndex = -Math.abs(Math.round(slideProgress)) + slides.length;
if (params.transformEl) {
$slideEl
.find(params.transformEl)
.css({
'transform-origin': 'center-bottom',
'backface-visibility': 'hidden',
'-webkit-backface-visibility': 'hidden',
})
Expand All @@ -92,7 +105,7 @@ export default function EffectCards({ swiper, extendParams, on }) {
const setTransition = (duration) => {
const { transformEl } = swiper.params.cardsEffect;
const $transitionElements = transformEl ? swiper.slides.find(transformEl) : swiper.slides;
$transitionElements.transition(duration);
$transitionElements.transition(duration).find('.swiper-slide-shadow').transition(duration);

virtualEffectTransitionEnd({ swiper, duration, transformEl });
};
Expand Down
1 change: 1 addition & 0 deletions src/modules/effect-cards/effect-cards.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
.swiper-slide {
transform-origin: center bottom;
backface-visibility: hidden;
overflow: hidden;
}
}
11 changes: 1 addition & 10 deletions src/modules/effect-coverflow/effect-coverflow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import $ from '../../shared/dom.js';
import createShadow from '../../shared/create-shadow.js';

export default function EffectCoverflow({ swiper, extendParams, on }) {
extendParams({
Expand All @@ -13,15 +13,6 @@ export default function EffectCoverflow({ swiper, extendParams, on }) {
},
});

const createShadow = (params, $slideEl, side) => {
const $shadowEl = $(`<div class="swiper-slide-shadow-${side}"></div>`);
if (params.transformEl) {
$slideEl.find(params.transformEl).append($shadowEl);
} else {
$slideEl.append($shadowEl);
}
return $shadowEl;
};
const setTranslate = () => {
const { width: swiperWidth, height: swiperHeight, slides, slidesSizesGrid } = swiper;
const params = swiper.params.coverflowEffect;
Expand Down
12 changes: 1 addition & 11 deletions src/modules/effect-flip/effect-flip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import $ from '../../shared/dom.js';
import createShadow from '../../shared/create-shadow.js';
import virtualEffectTransitionEnd from '../../shared/virtual-effect-transition-end.js';

export default function EffectFlip({ swiper, extendParams, on }) {
Expand All @@ -10,16 +10,6 @@ export default function EffectFlip({ swiper, extendParams, on }) {
},
});

const createShadow = (params, $slideEl, side) => {
const $shadowEl = $(`<div class="swiper-slide-shadow-${side}"></div>`);
if (params.transformEl) {
$slideEl.find(params.transformEl).append($shadowEl);
} else {
$slideEl.append($shadowEl);
}
return $shadowEl;
};

const setTranslate = () => {
const { slides, rtlTranslate: rtl } = swiper;
const params = swiper.params.flipEffect;
Expand Down
11 changes: 11 additions & 0 deletions src/shared/create-shadow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import $ from './dom.js';

export default function createShadow(params, $slideEl, side) {
const $shadowEl = $(`<div class="swiper-slide-shadow${side ? `-${side}` : ''}"></div>`);
if (params.transformEl) {
$slideEl.find(params.transformEl).append($shadowEl);
} else {
$slideEl.append($shadowEl);
}
return $shadowEl;
}
6 changes: 6 additions & 0 deletions src/types/modules/effect-cards.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export interface CardsEffectMethods {}
export interface CardsEffectEvents {}

export interface CardsEffectOptions {
/**
* Enables slides shadows
*
* @default true
*/
slideShadows?: boolean;
/**
* CSS selector of the element inside of the slide to transform instead of the slide itself. Useful to use with cssMode
*
Expand Down

0 comments on commit 09666c8

Please sign in to comment.