effect-coverflow.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import effectInit from '../../shared/effect-init.js';
  2. import effectTarget from '../../shared/effect-target.js';
  3. export default function EffectCoverflow({
  4. swiper,
  5. extendParams,
  6. on
  7. }) {
  8. extendParams({
  9. coverflowEffect: {
  10. rotate: 50,
  11. stretch: 0,
  12. depth: 100,
  13. scale: 1,
  14. modifier: 1,
  15. slideShadows: true,
  16. transformEl: null
  17. }
  18. });
  19. const setTranslate = () => {
  20. const {
  21. width: swiperWidth,
  22. height: swiperHeight,
  23. slides,
  24. slidesSizesGrid
  25. } = swiper;
  26. const params = swiper.params.coverflowEffect;
  27. const isHorizontal = swiper.isHorizontal();
  28. const transform = swiper.translate;
  29. const center = isHorizontal ? -transform + swiperWidth / 2 : -transform + swiperHeight / 2;
  30. const rotate = isHorizontal ? params.rotate : -params.rotate;
  31. const translate = params.depth; // Each slide offset from center
  32. for (let i = 0, length = slides.length; i < length; i += 1) {
  33. const $slideEl = slides[i];
  34. const slideSize = slidesSizesGrid[i];
  35. const slideOffset = $slideEl.swiperSlideOffset;
  36. const offsetMultiplier = (center - slideOffset - slideSize / 2) / slideSize * params.modifier;
  37. let rotateY = isHorizontal ? rotate * offsetMultiplier : 0;
  38. let rotateX = isHorizontal ? 0 : rotate * offsetMultiplier; // var rotateZ = 0
  39. let translateZ = -translate * Math.abs(offsetMultiplier);
  40. let stretch = params.stretch; // Allow percentage to make a relative stretch for responsive sliders
  41. if (typeof stretch === 'string' && stretch.indexOf('%') !== -1) {
  42. stretch = parseFloat(params.stretch) / 100 * slideSize;
  43. }
  44. let translateY = isHorizontal ? 0 : stretch * offsetMultiplier;
  45. let translateX = isHorizontal ? stretch * offsetMultiplier : 0;
  46. let scale = 1 - (1 - params.scale) * Math.abs(offsetMultiplier); // Fix for ultra small values
  47. if (Math.abs(translateX) < 0.001) translateX = 0;
  48. if (Math.abs(translateY) < 0.001) translateY = 0;
  49. if (Math.abs(translateZ) < 0.001) translateZ = 0;
  50. if (Math.abs(rotateY) < 0.001) rotateY = 0;
  51. if (Math.abs(rotateX) < 0.001) rotateX = 0;
  52. if (Math.abs(scale) < 0.001) scale = 0;
  53. const slideTransform =
  54. `translate3d(${translateX}px,${translateY}px,${translateZ}px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) scale(${scale})`;
  55. const $targetEl = effectTarget(params, $slideEl);
  56. $targetEl.transform(slideTransform);
  57. $slideEl.css({
  58. zIndex: -Math.abs(Math.round(offsetMultiplier)) + 1
  59. })
  60. if (swiper.params.willChange) {
  61. $targetEl.willChange("transform");
  62. }
  63. $slideEl.addClass('swiper-slide-coverflow')
  64. // if (params.slideShadows) {
  65. // // Set shadows
  66. // let $shadowBeforeEl = isHorizontal ? $slideEl.find('.swiper-slide-shadow-left') : $slideEl.find('.swiper-slide-shadow-top');
  67. // let $shadowAfterEl = isHorizontal ? $slideEl.find('.swiper-slide-shadow-right') : $slideEl.find('.swiper-slide-shadow-bottom');
  68. // if ($shadowBeforeEl.length === 0) {
  69. // $shadowBeforeEl = createShadow(params, $slideEl, isHorizontal ? 'left' : 'top');
  70. // }
  71. // if ($shadowAfterEl.length === 0) {
  72. // $shadowAfterEl = createShadow(params, $slideEl, isHorizontal ? 'right' : 'bottom');
  73. // }
  74. // if ($shadowBeforeEl.length) $shadowBeforeEl[0].style.opacity = offsetMultiplier > 0 ? offsetMultiplier : 0;
  75. // if ($shadowAfterEl.length) $shadowAfterEl[0].style.opacity = -offsetMultiplier > 0 ? -offsetMultiplier : 0;
  76. // }
  77. }
  78. };
  79. const setTransition = duration => {
  80. const {
  81. transformEl
  82. } = swiper.params.coverflowEffect;
  83. const $transitionElements = transformEl ? swiper.slides.find(transformEl) : swiper.slides;
  84. for (var i = 0; i < $transitionElements.length; i++) {
  85. $transitionElements[i].transition(duration);
  86. }
  87. };
  88. effectInit({
  89. effect: 'coverflow',
  90. swiper,
  91. on,
  92. setTranslate,
  93. setTransition,
  94. perspective: () => true,
  95. overwriteParams: () => ({
  96. watchSlidesProgress: true
  97. })
  98. });
  99. }