effect-cube.js 5.2 KB

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