effect-cube.js 5.6 KB

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