Slide.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { SLIDE } from '../../constants/types';
  2. import { EventInterface } from '../../constructors';
  3. import { Splide } from '../../core/Splide/Splide';
  4. import { Components, Options, TransitionComponent } from '../../types';
  5. import { abs, apply, style } from '../../utils';
  6. /**
  7. * The component for the slide transition.
  8. *
  9. * @since 3.0.0
  10. *
  11. * @param Splide - A Splide instance.
  12. * @param Components - A collection of components.
  13. * @param options - Options.
  14. *
  15. * @return A Transition component object.
  16. */
  17. export function Slide( Splide: Splide, Components: Components, options: Options ): TransitionComponent {
  18. const { bind } = EventInterface( Splide );
  19. const { Move, Controller, Scroll } = Components;
  20. const { list } = Components.Elements;
  21. const transition = apply( style, list, 'transition' );
  22. /**
  23. * Holds the `done` callback function.
  24. */
  25. let endCallback: () => void;
  26. /**
  27. * Called when the component is mounted.
  28. */
  29. function mount(): void {
  30. bind( list, 'transitionend', e => {
  31. if ( e.target === list && endCallback ) {
  32. cancel();
  33. endCallback();
  34. }
  35. } );
  36. }
  37. /**
  38. * Starts the transition.
  39. * The Move component calls this method just before the slider moves.
  40. *
  41. * @param index - A destination index.
  42. * @param done - The callback function that must be called after the transition ends.
  43. */
  44. function start( index: number, done: () => void ): void {
  45. const destination = Move.toPosition( index, true );
  46. const position = Move.getPosition();
  47. const speed = getSpeed( index );
  48. if ( abs( destination - position ) >= 1 && speed >= 1 ) {
  49. if ( options.useScroll ) {
  50. Scroll.scroll( destination, speed, false, done );
  51. } else {
  52. transition( `transform ${ speed }ms ${ options.easing }` );
  53. Move.translate( destination, true );
  54. endCallback = done;
  55. }
  56. } else {
  57. Move.jump( index );
  58. done();
  59. }
  60. }
  61. /**
  62. * Cancels the transition.
  63. */
  64. function cancel(): void {
  65. transition( '' );
  66. Scroll.cancel();
  67. }
  68. /**
  69. * Returns the transition speed.
  70. *
  71. * @param index - A destination index.
  72. */
  73. function getSpeed( index: number ): number {
  74. const { rewindSpeed } = options;
  75. if ( Splide.is( SLIDE ) && rewindSpeed ) {
  76. const prev = Controller.getIndex( true );
  77. const end = Controller.getEnd();
  78. if ( ( prev === 0 && index >= end ) || ( prev >= end && index === 0 ) ) {
  79. return rewindSpeed;
  80. }
  81. }
  82. return options.speed;
  83. }
  84. return {
  85. mount,
  86. start,
  87. cancel,
  88. };
  89. }