effect-cube.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 setTranslate = () => {
  17. const {
  18. $el,
  19. $wrapperEl,
  20. slides,
  21. width: swiperWidth,
  22. height: swiperHeight,
  23. rtlTranslate: rtl,
  24. size: swiperSize,
  25. browser
  26. } = swiper;
  27. const params = swiper.params.cubeEffect;
  28. const isHorizontal = swiper.isHorizontal();
  29. const isVirtual = swiper.virtual && swiper.params.virtual.enabled;
  30. let wrapperRotate = 0;
  31. let $cubeShadowEl;
  32. if (params.shadow) {
  33. if (isHorizontal) {
  34. $cubeShadowEl = $wrapperEl.find('.swiper-cube-shadow');
  35. if ($cubeShadowEl.length === 0) {
  36. $cubeShadowEl = $('<div class="swiper-cube-shadow"></div>');
  37. $wrapperEl.append($cubeShadowEl);
  38. }
  39. $cubeShadowEl.css({
  40. height: `${swiperWidth}px`
  41. });
  42. } else {
  43. $cubeShadowEl = $el.find('.swiper-cube-shadow');
  44. if ($cubeShadowEl.length === 0) {
  45. $cubeShadowEl = $('<div class="swiper-cube-shadow"></div>');
  46. $el.append($cubeShadowEl);
  47. }
  48. }
  49. }
  50. for (let i = 0; i < slides.length; i += 1) {
  51. const $slideEl = slides.eq(i);
  52. let slideIndex = i;
  53. if (isVirtual) {
  54. slideIndex = parseInt($slideEl.attr('data-swiper-slide-index'), 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[0].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(${isHorizontal ? slideAngle : 0}deg) translate3d(${tx}px, ${ty}px, ${tz}px)`;
  87. if (progress <= 1 && progress > -1) {
  88. wrapperRotate = slideIndex * 90 + progress * 90;
  89. if (rtl) wrapperRotate = -slideIndex * 90 - progress * 90;
  90. }
  91. $slideEl.transform(transform);
  92. if (params.slideShadows) {
  93. // Set shadows
  94. let shadowBefore = isHorizontal ? $slideEl.find('.swiper-slide-shadow-left') : $slideEl.find('.swiper-slide-shadow-top');
  95. let shadowAfter = isHorizontal ? $slideEl.find('.swiper-slide-shadow-right') : $slideEl.find('.swiper-slide-shadow-bottom');
  96. if (shadowBefore.length === 0) {
  97. shadowBefore = $(`<div class="swiper-slide-shadow-${isHorizontal ? 'left' : 'top'}"></div>`);
  98. $slideEl.append(shadowBefore);
  99. }
  100. if (shadowAfter.length === 0) {
  101. shadowAfter = $(`<div class="swiper-slide-shadow-${isHorizontal ? 'right' : 'bottom'}"></div>`);
  102. $slideEl.append(shadowAfter);
  103. }
  104. if (shadowBefore.length) shadowBefore[0].style.opacity = Math.max(-progress, 0);
  105. if (shadowAfter.length) shadowAfter[0].style.opacity = Math.max(progress, 0);
  106. }
  107. }
  108. $wrapperEl.css({
  109. '-webkit-transform-origin': `50% 50% -${swiperSize / 2}px`,
  110. 'transform-origin': `50% 50% -${swiperSize / 2}px`
  111. });
  112. if (params.shadow) {
  113. if (isHorizontal) {
  114. $cubeShadowEl.transform(`translate3d(0px, ${swiperWidth / 2 + params.shadowOffset}px, ${-swiperWidth / 2}px) rotateX(90deg) rotateZ(0deg) scale(${params.shadowScale})`);
  115. } else {
  116. const shadowAngle = Math.abs(wrapperRotate) - Math.floor(Math.abs(wrapperRotate) / 90) * 90;
  117. const multiplier = 1.5 - (Math.sin(shadowAngle * 2 * Math.PI / 360) / 2 + Math.cos(shadowAngle * 2 * Math.PI / 360) / 2);
  118. const scale1 = params.shadowScale;
  119. const scale2 = params.shadowScale / multiplier;
  120. const offset = params.shadowOffset;
  121. $cubeShadowEl.transform(`scale3d(${scale1}, 1, ${scale2}) translate3d(0px, ${swiperHeight / 2 + offset}px, ${-swiperHeight / 2 / scale2}px) rotateX(-90deg)`);
  122. }
  123. }
  124. const zFactor = browser.isSafari || browser.isWebView ? -swiperSize / 2 : 0;
  125. $wrapperEl.transform(`translate3d(0px,0,${zFactor}px) rotateX(${swiper.isHorizontal() ? 0 : wrapperRotate}deg) rotateY(${swiper.isHorizontal() ? -wrapperRotate : 0}deg)`);
  126. };
  127. const setTransition = duration => {
  128. const {
  129. $el,
  130. slides
  131. } = swiper;
  132. slides.transition(duration).find('.swiper-slide-shadow-top, .swiper-slide-shadow-right, .swiper-slide-shadow-bottom, .swiper-slide-shadow-left').transition(duration);
  133. if (swiper.params.cubeEffect.shadow && !swiper.isHorizontal()) {
  134. $el.find('.swiper-cube-shadow').transition(duration);
  135. }
  136. };
  137. effectInit({
  138. effect: 'cube',
  139. swiper,
  140. on,
  141. setTranslate,
  142. setTransition,
  143. perspective: () => true,
  144. overwriteParams: () => ({
  145. slidesPerView: 1,
  146. slidesPerGroup: 1,
  147. watchSlidesProgress: true,
  148. resistanceRatio: 0,
  149. spaceBetween: 0,
  150. centeredSlides: false,
  151. virtualTranslate: true
  152. })
  153. });
  154. }