effect-cube.js 6.3 KB

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