effect-cards.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import effectInit from '../../shared/effect-init.js';
  2. import effectTarget from '../../shared/effect-target.js';
  3. import effectVirtualTransitionEnd from '../../shared/effect-virtual-transition-end.js';
  4. export default function EffectCards({
  5. swiper,
  6. extendParams,
  7. on
  8. }) {
  9. extendParams({
  10. cardsEffect: {
  11. slideShadows: true,
  12. transformEl: null,
  13. rotate: true,
  14. perSlideRotate: 2,
  15. perSlideOffset: 8,
  16. },
  17. });
  18. const setTranslate = () => {
  19. const {
  20. slides,
  21. activeIndex
  22. } = swiper;
  23. const params = swiper.params.cardsEffect;
  24. const {
  25. startTranslate,
  26. isTouched
  27. } = swiper.touchEventsData;
  28. const currentTranslate = swiper.translate;
  29. for (let i = 0; i < slides.length; i += 1) {
  30. const $slideEl = slides[i];
  31. const slideProgress = $slideEl.progress;
  32. const progress = Math.min(Math.max(slideProgress, -4), 4);
  33. let offset = $slideEl.swiperSlideOffset;
  34. if (swiper.params.centeredSlides && !swiper.params.cssMode) {
  35. swiper.$wrapperEl.transform(`translateX(${swiper.minTranslate()}px)`);
  36. }
  37. if (swiper.params.centeredSlides && swiper.params.cssMode) {
  38. offset -= slides.swiperSlideOffset;
  39. }
  40. let tX = swiper.params.cssMode ? -offset - swiper.translate : -offset;
  41. let tY = 0;
  42. const tZ = -100 * Math.abs(progress);
  43. let scale = 1;
  44. let rotate = -params.perSlideRotate * progress;
  45. let tXAdd = params.perSlideOffset - Math.abs(progress) * 0.75;
  46. const isSwipeToNext =
  47. (i === activeIndex || i === activeIndex - 1) &&
  48. progress > 0 &&
  49. progress < 1 &&
  50. (isTouched || swiper.params.cssMode) &&
  51. currentTranslate < startTranslate;
  52. const isSwipeToPrev =
  53. (i === activeIndex || i === activeIndex + 1) &&
  54. progress < 0 &&
  55. progress > -1 &&
  56. (isTouched || swiper.params.cssMode) &&
  57. currentTranslate > startTranslate;
  58. if (isSwipeToNext || isSwipeToPrev) {
  59. const subProgress = (1 - Math.abs((Math.abs(progress) - 0.5) / 0.5)) ** 0.5;
  60. rotate += -28 * progress * subProgress;
  61. scale += -0.5 * subProgress;
  62. tXAdd += 96 * subProgress;
  63. tY = `${-25 * subProgress * Math.abs(progress)}%`;
  64. }
  65. if (progress < 0) {
  66. // next
  67. tX = `calc(${tX}px + (${tXAdd * Math.abs(progress)}%))`;
  68. } else if (progress > 0) {
  69. // prev
  70. tX = `calc(${tX}px + (-${tXAdd * Math.abs(progress)}%))`;
  71. } else {
  72. tX = `${tX}px`;
  73. }
  74. if (!swiper.isHorizontal()) {
  75. const prevY = tY;
  76. tY = tX;
  77. tX = prevY;
  78. }
  79. const scaleString =
  80. progress < 0 ? `${1 + (1 - scale) * progress}` : `${1 - (1 - scale) * progress}`;
  81. const transform =
  82. `translate3d(${tX}, ${tY}, ${tZ}px) rotateZ(${params.rotate ? rotate : 0}deg) scale(${scaleString})`;
  83. $slideEl.css({
  84. zIndex: -Math.abs(Math.round(slideProgress)) + slides.length
  85. })
  86. const $targetEl = effectTarget(params, $slideEl);
  87. $targetEl.transform(transform);
  88. if (swiper.params.willChange) {
  89. $targetEl.willChange("transform");
  90. }
  91. slides[i].addClass('swiper-slide-cards')
  92. }
  93. };
  94. const setTransition = (duration) => {
  95. const {
  96. transformEl
  97. } = swiper.params.cardsEffect;
  98. const $transitionElements = transformEl ? swiper.slides.find(transformEl) : swiper.slides;
  99. for (let i = 0; i < $transitionElements.length; i += 1) {
  100. $transitionElements[i].transition(duration);
  101. }
  102. effectVirtualTransitionEnd({
  103. swiper,
  104. duration,
  105. transformEl
  106. });
  107. };
  108. effectInit({
  109. effect: 'cards',
  110. swiper,
  111. on,
  112. setTranslate,
  113. setTransition,
  114. perspective: () => true,
  115. overwriteParams: () => ({
  116. watchSlidesProgress: true,
  117. virtualTranslate: !swiper.params.cssMode,
  118. }),
  119. });
  120. }