-
Notifications
You must be signed in to change notification settings - Fork 0
/
posters-slider.js
48 lines (44 loc) · 1.24 KB
/
posters-slider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// eslint-disable-next-line
function createPostersSlider(el) {
const swiperEl = el.querySelector('.swiper');
const calcNextOffset = () => {
const parentWidth = swiperEl.parentElement.offsetWidth;
const swiperWidth = swiperEl.offsetWidth;
let nextOffset =
(parentWidth - (parentWidth - swiperWidth) / 2) / swiperWidth;
nextOffset = Math.max(nextOffset, 1);
return `${nextOffset * 100}%`;
};
const postersSwiper = new Swiper(swiperEl, {
effect: 'creative',
speed: 600,
resistanceRatio: 0,
grabCursor: true,
parallax: true,
creativeEffect: {
limitProgress: 3,
perspective: true,
shadowPerProgress: true,
prev: {
shadow: true,
translate: ['-15%', 0, -200],
},
next: {
translate: [calcNextOffset(), 0, 0],
},
},
});
const onResize = () => {
if (!postersSwiper || postersSwiper.destroyed) return;
// prettier-ignore
postersSwiper.params.creativeEffect.next.translate = [calcNextOffset(), 0, 0];
if (
postersSwiper.params.resizeObserver &&
typeof window.ResizeObserver !== 'undefined'
) {
postersSwiper.update();
}
};
window.addEventListener('resize', onResize);
return postersSwiper;
}