effect-cube.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import $ from '../../shared/dom.js';
  2. import effectInit from '../../shared/effect-init.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.find('.swiper-slide-shadow-left') : $slideEl.find('.swiper-slide-shadow-top');
  18. let shadowAfter = isHorizontal ? $slideEl.find('.swiper-slide-shadow-right') : $slideEl.find('.swiper-slide-shadow-bottom');
  19. if (shadowBefore.length === 0) {
  20. shadowBefore = $(`<div class="swiper-slide-shadow-${isHorizontal ? 'left' : 'top'}"></div>`);
  21. $slideEl.append(shadowBefore);
  22. }
  23. if (shadowAfter.length === 0) {
  24. shadowAfter = $(`<div class="swiper-slide-shadow-${isHorizontal ? 'right' : 'bottom'}"></div>`);
  25. $slideEl.append(shadowAfter);
  26. }
  27. if (shadowBefore.length) shadowBefore[0].style.opacity = Math.max(-progress, 0);
  28. if (shadowAfter.length) shadowAfter[0].style.opacity = Math.max(progress, 0);
  29. };
  30. const recreateShadows = () => {
  31. // create new ones
  32. const isHorizontal = swiper.isHorizontal();
  33. swiper.slides.each(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 = $wrapperEl.find('.swiper-cube-shadow');
  57. if ($cubeShadowEl.length === 0) {
  58. $cubeShadowEl = $('<div class="swiper-cube-shadow"></div>');
  59. $wrapperEl.append($cubeShadowEl);
  60. }
  61. $cubeShadowEl.css({
  62. height: `${swiperWidth}px`
  63. });
  64. } else {
  65. $cubeShadowEl = $el.find('.swiper-cube-shadow');
  66. if ($cubeShadowEl.length === 0) {
  67. $cubeShadowEl = $('<div class="swiper-cube-shadow"></div>');
  68. $el.append($cubeShadowEl);
  69. }
  70. }
  71. }
  72. for (let i = 0; i < slides.length; i += 1) {
  73. const $slideEl = slides.eq(i);
  74. let slideIndex = i;
  75. if (isVirtual) {
  76. slideIndex = parseInt($slideEl.attr('data-swiper-slide-index'), 10);
  77. }
  78. let slideAngle = slideIndex * 90;
  79. let round = Math.floor(slideAngle / 360);
  80. if (rtl) {
  81. slideAngle = -slideAngle;
  82. round = Math.floor(-slideAngle / 360);
  83. }
  84. const progress = Math.max(Math.min($slideEl[0].progress, 1), -1);
  85. let tx = 0;
  86. let ty = 0;
  87. let tz = 0;
  88. if (slideIndex % 4 === 0) {
  89. tx = -round * 4 * swiperSize;
  90. tz = 0;
  91. } else if ((slideIndex - 1) % 4 === 0) {
  92. tx = 0;
  93. tz = -round * 4 * swiperSize;
  94. } else if ((slideIndex - 2) % 4 === 0) {
  95. tx = swiperSize + round * 4 * swiperSize;
  96. tz = swiperSize;
  97. } else if ((slideIndex - 3) % 4 === 0) {
  98. tx = -swiperSize;
  99. tz = 3 * swiperSize + swiperSize * 4 * round;
  100. }
  101. if (rtl) {
  102. tx = -tx;
  103. }
  104. if (!isHorizontal) {
  105. ty = tx;
  106. tx = 0;
  107. }
  108. const transform = `rotateX(${isHorizontal ? 0 : -slideAngle}deg) rotateY(${isHorizontal ? slideAngle : 0}deg) translate3d(${tx}px, ${ty}px, ${tz}px)`;
  109. if (progress <= 1 && progress > -1) {
  110. wrapperRotate = slideIndex * 90 + progress * 90;
  111. if (rtl) wrapperRotate = -slideIndex * 90 - progress * 90;
  112. }
  113. $slideEl.transform(transform);
  114. if (params.slideShadows) {
  115. createSlideShadows($slideEl, progress, isHorizontal);
  116. }
  117. }
  118. $wrapperEl.css({
  119. '-webkit-transform-origin': `50% 50% -${swiperSize / 2}px`,
  120. 'transform-origin': `50% 50% -${swiperSize / 2}px`
  121. });
  122. if (params.shadow) {
  123. if (isHorizontal) {
  124. $cubeShadowEl.transform(`translate3d(0px, ${swiperWidth / 2 + params.shadowOffset}px, ${-swiperWidth / 2}px) rotateX(90deg) rotateZ(0deg) scale(${params.shadowScale})`);
  125. } else {
  126. const shadowAngle = Math.abs(wrapperRotate) - Math.floor(Math.abs(wrapperRotate) / 90) * 90;
  127. const multiplier = 1.5 - (Math.sin(shadowAngle * 2 * Math.PI / 360) / 2 + Math.cos(shadowAngle * 2 * Math.PI / 360) / 2);
  128. const scale1 = params.shadowScale;
  129. const scale2 = params.shadowScale / multiplier;
  130. const offset = params.shadowOffset;
  131. $cubeShadowEl.transform(`scale3d(${scale1}, 1, ${scale2}) translate3d(0px, ${swiperHeight / 2 + offset}px, ${-swiperHeight / 2 / scale2}px) rotateX(-90deg)`);
  132. }
  133. }
  134. const zFactor = browser.isSafari || browser.isWebView ? -swiperSize / 2 : 0;
  135. $wrapperEl.transform(`translate3d(0px,0,${zFactor}px) rotateX(${swiper.isHorizontal() ? 0 : wrapperRotate}deg) rotateY(${swiper.isHorizontal() ? -wrapperRotate : 0}deg)`);
  136. $wrapperEl[0].style.setProperty('--swiper-cube-translate-z', `${zFactor}px`);
  137. };
  138. const setTransition = duration => {
  139. const {
  140. $el,
  141. slides
  142. } = swiper;
  143. slides.transition(duration).find('.swiper-slide-shadow-top, .swiper-slide-shadow-right, .swiper-slide-shadow-bottom, .swiper-slide-shadow-left').transition(duration);
  144. if (swiper.params.cubeEffect.shadow && !swiper.isHorizontal()) {
  145. $el.find('.swiper-cube-shadow').transition(duration);
  146. }
  147. };
  148. effectInit({
  149. effect: 'cube',
  150. swiper,
  151. on,
  152. setTranslate,
  153. setTransition,
  154. recreateShadows,
  155. getEffectParams: () => swiper.params.cubeEffect,
  156. perspective: () => true,
  157. overwriteParams: () => ({
  158. slidesPerView: 1,
  159. slidesPerGroup: 1,
  160. watchSlidesProgress: true,
  161. resistanceRatio: 0,
  162. spaceBetween: 0,
  163. centeredSlides: false,
  164. virtualTranslate: true
  165. })
  166. });
  167. }