effect-cube.js
5.4 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import effectInit from '../../shared/effect-init.js';
export default function EffectCube({
swiper,
extendParams,
on
}) {
extendParams({
cubeEffect: {
slideShadows: true,
shadow: true,
shadowOffset: 20,
shadowScale: 0.94,
},
});
const setTranslate = () => {
const {
$el,
$wrapperEl,
slides,
width: swiperWidth,
height: swiperHeight,
rtlTranslate: rtl,
size: swiperSize,
browser,
} = swiper;
const params = swiper.params.cubeEffect;
const isHorizontal = swiper.isHorizontal();
const isVirtual = swiper.virtual && swiper.params.virtual.enabled;
let wrapperRotate = 0;
let $cubeShadowEl;
if (params.shadow) {
if (isHorizontal) {
// $cubeShadowEl = $wrapperEl.find('.swiper-cube-shadow');
if (!swiper.native.cubeShadowShowWrapper) {
swiper.$wrapperEl.updateData({
cubeShadowShowWrapper: true
})
}
swiper.$wrapperEl.cubeShadowCss({
height: `${swiperWidth}px`
});
} else {
if (!swiper.native.cubeShadowShowRoot) {
swiper.$wrapperEl.updateData({
cubeShadowShowRoot: true
})
}
}
}
for (let i = 0; i < slides.length; i += 1) {
const $slideEl = slides[i];
let slideIndex = i;
if (isVirtual) {
slideIndex = parseInt(swiper.activeIndex, 10);
}
let slideAngle = slideIndex * 90;
let round = Math.floor(slideAngle / 360);
if (rtl) {
slideAngle = -slideAngle;
round = Math.floor(-slideAngle / 360);
}
const progress = Math.max(Math.min($slideEl.progress, 1), -1);
let tx = 0;
let ty = 0;
let tz = 0;
if (slideIndex % 4 === 0) {
tx = -round * 4 * swiperSize;
tz = 0;
} else if ((slideIndex - 1) % 4 === 0) {
tx = 0;
tz = -round * 4 * swiperSize;
} else if ((slideIndex - 2) % 4 === 0) {
tx = swiperSize + round * 4 * swiperSize;
tz = swiperSize;
} else if ((slideIndex - 3) % 4 === 0) {
tx = -swiperSize;
tz = 3 * swiperSize + swiperSize * 4 * round;
}
if (rtl) {
tx = -tx;
}
if (!isHorizontal) {
ty = tx;
tx = 0;
}
const transform = `rotateX(${isHorizontal ? 0 : -slideAngle}deg) rotateY(${
isHorizontal ? slideAngle : 0
}deg) translate3d(${tx}px, ${ty}px, ${tz}px)`;
if (progress <= 1 && progress > -1) {
wrapperRotate = slideIndex * 90 + progress * 90;
if (rtl) wrapperRotate = -slideIndex * 90 - progress * 90;
}
$slideEl.transform(transform);
// if (params.slideShadows) {
// // Set shadows
// let shadowBefore = isHorizontal ?
// $slideEl.find('.swiper-slide-shadow-left') :
// $slideEl.find('.swiper-slide-shadow-top');
// let shadowAfter = isHorizontal ?
// $slideEl.find('.swiper-slide-shadow-right') :
// $slideEl.find('.swiper-slide-shadow-bottom');
// if (shadowBefore.length === 0) {
// shadowBefore = $(
// `<div class="swiper-slide-shadow-${isHorizontal ? 'left' : 'top'}"></div>`,
// );
// $slideEl.append(shadowBefore);
// }
// if (shadowAfter.length === 0) {
// shadowAfter = $(
// `<div class="swiper-slide-shadow-${isHorizontal ? 'right' : 'bottom'}"></div>`,
// );
// $slideEl.append(shadowAfter);
// }
// if (shadowBefore.length) shadowBefore[0].style.opacity = Math.max(-progress, 0);
// if (shadowAfter.length) shadowAfter[0].style.opacity = Math.max(progress, 0);
// }
$slideEl.addClass('swiper-slide-cube')
}
$wrapperEl.css({
'-webkit-transform-origin': `50% 50% -${swiperSize / 2}px`,
'transform-origin': `50% 50% -${swiperSize / 2}px`,
});
if (params.shadow) {
if (isHorizontal) {
swiper.$wrapperEl.cubeShadowTransform(
`translate3d(0px, ${swiperWidth / 2 + params.shadowOffset}px, ${
-swiperWidth / 2
}px) rotateX(90deg) rotateZ(0deg) scale(${params.shadowScale})`,
);
} else {
const shadowAngle = Math.abs(wrapperRotate) - Math.floor(Math.abs(wrapperRotate) / 90) * 90;
const multiplier =
1.5 -
(Math.sin((shadowAngle * 2 * Math.PI) / 360) / 2 +
Math.cos((shadowAngle * 2 * Math.PI) / 360) / 2);
const scale1 = params.shadowScale;
const scale2 = params.shadowScale / multiplier;
const offset = params.shadowOffset;
swiper.$wrapperEl.cubeShadowTransform(
`scale3d(${scale1}, 1, ${scale2}) translate3d(0px, ${swiperHeight / 2 + offset}px, ${
-swiperHeight / 2 / scale2
}px) rotateX(-90deg)`,
);
}
}
const zFactor = browser.isSafari || browser.isWebView ? -swiperSize / 2 : 0;
$wrapperEl.transform(
`translate3d(0px,0,${zFactor}px) rotateX(${
swiper.isHorizontal() ? 0 : wrapperRotate
}deg) rotateY(${swiper.isHorizontal() ? -wrapperRotate : 0}deg)`,
);
};
const setTransition = (duration) => {
const {
$el,
slides
} = swiper;
for (var i = 0; i < slides.length; i++) {
slides[i].transition(duration)
}
if (swiper.params.cubeEffect.shadow && !swiper.isHorizontal()) {
swiper.$wrapperEl.cubeShadowTransition(duration);
}
};
effectInit({
effect: 'cube',
swiper,
on,
setTranslate,
setTransition,
perspective: () => true,
overwriteParams: () => ({
slidesPerView: 1,
slidesPerGroup: 1,
watchSlidesProgress: true,
resistanceRatio: 0,
spaceBetween: 0,
centeredSlides: false,
virtualTranslate: true,
}),
});
}