effect-flip.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 EffectFlip({
  5. swiper,
  6. extendParams,
  7. on
  8. }) {
  9. extendParams({
  10. flipEffect: {
  11. slideShadows: true,
  12. limitRotation: true,
  13. transformEl: null,
  14. },
  15. });
  16. const setTranslate = () => {
  17. const {
  18. slides,
  19. rtlTranslate: rtl
  20. } = swiper;
  21. const params = swiper.params.flipEffect;
  22. for (let i = 0; i < slides.length; i += 1) {
  23. const $slideEl = slides[i];
  24. let progress = $slideEl.progress;
  25. if (swiper.params.flipEffect.limitRotation) {
  26. progress = Math.max(Math.min($slideEl.progress, 1), -1);
  27. }
  28. const offset = $slideEl.swiperSlideOffset;
  29. const rotate = -180 * progress;
  30. let rotateY = rotate;
  31. let rotateX = 0;
  32. let tx = swiper.params.cssMode ? -offset - swiper.translate : -offset;
  33. let ty = 0;
  34. if (!swiper.isHorizontal()) {
  35. ty = tx;
  36. tx = 0;
  37. rotateX = -rotateY;
  38. rotateY = 0;
  39. } else if (rtl) {
  40. rotateY = -rotateY;
  41. }
  42. $slideEl.css({
  43. zIndex: -Math.abs(Math.round(progress)) + slides.length
  44. })
  45. // if (params.slideShadows) {
  46. // // Set shadows
  47. // let shadowBefore = swiper.isHorizontal()
  48. // ? $slideEl.find('.swiper-slide-shadow-left')
  49. // : $slideEl.find('.swiper-slide-shadow-top');
  50. // let shadowAfter = swiper.isHorizontal()
  51. // ? $slideEl.find('.swiper-slide-shadow-right')
  52. // : $slideEl.find('.swiper-slide-shadow-bottom');
  53. // if (shadowBefore.length === 0) {
  54. // shadowBefore = createShadow(params, $slideEl, swiper.isHorizontal() ? 'left' : 'top');
  55. // }
  56. // if (shadowAfter.length === 0) {
  57. // shadowAfter = createShadow(params, $slideEl, swiper.isHorizontal() ? 'right' : 'bottom');
  58. // }
  59. // if (shadowBefore.length) shadowBefore[0].style.opacity = Math.max(-progress, 0);
  60. // if (shadowAfter.length) shadowAfter[0].style.opacity = Math.max(progress, 0);
  61. // }
  62. const transform = `translate3d(${tx}px, ${ty}px, 0px) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
  63. const $targetEl = effectTarget(params, $slideEl);
  64. $targetEl.transform(transform);
  65. if (swiper.params.willChange) {
  66. $targetEl.willChange("transform");
  67. }
  68. slides[i].addClass('swiper-slide-flip')
  69. }
  70. };
  71. const setTransition = (duration) => {
  72. const {
  73. transformEl
  74. } = swiper.params.flipEffect;
  75. const $transitionElements = transformEl ? swiper.slides.find(transformEl) : swiper.slides;
  76. for (let i = 0; i < $transitionElements.length; i += 1) {
  77. $transitionElements[i].transition(duration);
  78. }
  79. effectVirtualTransitionEnd({
  80. swiper,
  81. duration,
  82. transformEl
  83. });
  84. };
  85. effectInit({
  86. effect: 'flip',
  87. swiper,
  88. on,
  89. setTranslate,
  90. setTransition,
  91. perspective: () => true,
  92. overwriteParams: () => ({
  93. slidesPerView: 1,
  94. slidesPerGroup: 1,
  95. watchSlidesProgress: true,
  96. spaceBetween: 0,
  97. virtualTranslate: !swiper.params.cssMode,
  98. }),
  99. });
  100. }