effect-init.mjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. function effectInit(params) {
  2. const {
  3. effect,
  4. swiper,
  5. on,
  6. setTranslate,
  7. setTransition,
  8. overwriteParams,
  9. perspective,
  10. recreateShadows,
  11. getEffectParams
  12. } = params;
  13. on('beforeInit', () => {
  14. if (swiper.params.effect !== effect) return;
  15. swiper.classNames.push(`${swiper.params.containerModifierClass}${effect}`);
  16. if (perspective && perspective()) {
  17. swiper.classNames.push(`${swiper.params.containerModifierClass}3d`);
  18. }
  19. const overwriteParamsResult = overwriteParams ? overwriteParams() : {};
  20. Object.assign(swiper.params, overwriteParamsResult);
  21. Object.assign(swiper.originalParams, overwriteParamsResult);
  22. });
  23. on('setTranslate', () => {
  24. if (swiper.params.effect !== effect) return;
  25. setTranslate();
  26. });
  27. on('setTransition', (_s, duration) => {
  28. if (swiper.params.effect !== effect) return;
  29. setTransition(duration);
  30. });
  31. on('transitionEnd', () => {
  32. if (swiper.params.effect !== effect) return;
  33. if (recreateShadows) {
  34. if (!getEffectParams || !getEffectParams().slideShadows) return;
  35. // remove shadows
  36. swiper.slides.forEach(slideEl => {
  37. slideEl.querySelectorAll('.swiper-slide-shadow-top, .swiper-slide-shadow-right, .swiper-slide-shadow-bottom, .swiper-slide-shadow-left').forEach(shadowEl => shadowEl.remove());
  38. });
  39. // create new one
  40. recreateShadows();
  41. }
  42. });
  43. let requireUpdateOnVirtual;
  44. on('virtualUpdate', () => {
  45. if (swiper.params.effect !== effect) return;
  46. if (!swiper.slides.length) {
  47. requireUpdateOnVirtual = true;
  48. }
  49. requestAnimationFrame(() => {
  50. if (requireUpdateOnVirtual && swiper.slides && swiper.slides.length) {
  51. setTranslate();
  52. requireUpdateOnVirtual = false;
  53. }
  54. });
  55. });
  56. }
  57. export { effectInit as e };