Transition
Transition Component
The Transition component is a special component that changes slides by a transition effects. Splide has 2 built in Transition components, "fade" and "slide".
Creating A Transition
This guide assumes that you have the knowledge of Extension.
You can create a Transition component in the same way as an extension.
The only difference between them is the Transition component requires the start() and cancel() methods.
exportfunctionMyTransition(Splide,Components){// Optionalfunctionmount(){}// Requiredfunctionstart(index,done){}// Requiredfunctioncancel(){}// Optionalfunctiondestroy(){}return{mount,start,cancel,destroy,};}JavaScript
export function MyTransition( Splide, Components ) {
// Optional
function mount() {
}
// Required
function start( index, done ) {
}
// Required
function cancel() {
}
// Optional
function destroy() {
}
return {
mount,
start,
cancel,
destroy,
};
}
The Move core component calls the start() method for moving the slider.
The Transition component is supposed to move the slider to the specified index,
and call the done callback after the transition ends.
index | number | A destination index. |
|---|---|---|
done | () => void | A function that must be called after the transition ends. |
The cancel() method will be called when the transition is interrupted for some reasons.
For example, the user may start dragging the slider while transitioning if the waitForTransition option is false.
Registration
To register the created Transition component, pass it to the Splide#mount() method as the 2nd argument:
importSplidefrom'@splidejs/splide';import{MyTransition}from'...';newSplide('#splide').mount({},MyTransition);JavaScript
import Splide from '@splidejs/splide';
import { MyTransition } from '...';
new Splide( '#splide' ).mount( {}, MyTransition );
Example
Here is a small example that moves the slider by using the CSS transition:
123456789101112131415161718192021222324252627282930313233343536373839404142434445import{EventInterface}from'@splidejs/splide';exportfunctionMyTransition(Splide,Components){const{bind}=EventInterface(Splide);const{Move}=Components;const{list}=Components.Elements;letendCallback;functionmount(){bind(list,'transitionend',e=>{if(e.target===list&&endCallback){// Removes the transition propertycancel();// Calls the `done` callbackendCallback();}});}functionstart(index,done){// Converts the index to the positionconstdestination=Move.toPosition(index,true);// Applies the CSS transitionlist.style.transition='transform 800ms cubic-bezier(.44,.65,.07,1.01)';// Moves the slider to the destination.Move.translate(destination);// Keeps the callback to invoke later.endCallback=done;}functioncancel(){list.style.transition='';}return{mount,start,cancel,};}JavaScript
import { EventInterface } from '@splidejs/splide';
export function MyTransition( Splide, Components ) {
const { bind } = EventInterface( Splide );
const { Move } = Components;
const { list } = Components.Elements;
let endCallback;
function mount() {
bind( list, 'transitionend', e => {
if ( e.target === list && endCallback ) {
// Removes the transition property
cancel();
// Calls the `done` callback
endCallback();
}
} );
}
function start( index, done ) {
// Converts the index to the position
const destination = Move.toPosition( index, true );
// Applies the CSS transition
list.style.transition = 'transform 800ms cubic-bezier(.44,.65,.07,1.01)';
// Moves the slider to the destination.
Move.translate( destination );
// Keeps the callback to invoke later.
endCallback = done;
}
function cancel() {
list.style.transition = '';
}
return {
mount,
start,
cancel,
};
}
The result will be like this:
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09