Animations and Transitions

30 Jun 202318 minutes to read

Animations

Fade animation

In Carousel, two built-in animations are provided for slide transitions. You can disable animation using the AnimationEffect property. By default, Slide animation is applied for the transition between slides.

The following demo depicts the example for Fade animation,

<div class="control-container">
    <ejs-carousel id="defaultCarousel" animationEffect="Fade">
        <e-carousel-items>
            <e-carousel-item template='<div class="slide-content">Slide 1</div>'></e-carousel-item>
            <e-carousel-item template='<div class="slide-content">Slide 2</div>'></e-carousel-item>
            <e-carousel-item template='<div class="slide-content">Slide 3</div>'></e-carousel-item>
            <e-carousel-item template='<div class="slide-content">Slide 4</div>'></e-carousel-item>
            <e-carousel-item template='<div class="slide-content">Slide 5</div>'></e-carousel-item>
        </e-carousel-items>
    </ejs-carousel>
</div>
<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>

Carousel fade animation

Custom animation

In Carousel, you can use customized animation effects for slide transitions using the Custom option of the AnimationEffect property and apply custom animation css via cssClassproperty.

The following demo depicts the example for parallax custom animation,

<div class="control-container">
    <ejs-carousel id="carousel" animationEffect="Custom" cssClass="parallax">
        <e-carousel-items>
            <e-carousel-item template='<div class="slide-content">Slide 1</div>'></e-carousel-item>
            <e-carousel-item template='<div class="slide-content">Slide 2</div>'></e-carousel-item>
            <e-carousel-item template='<div class="slide-content">Slide 3</div>'></e-carousel-item>
            <e-carousel-item template='<div class="slide-content">Slide 4</div>'></e-carousel-item>
            <e-carousel-item template='<div class="slide-content">Slide 5</div>'></e-carousel-item>
        </e-carousel-items>
    </ejs-carousel>
</div>
<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }

    /* Parallax animation */
    .parallax .e-carousel-item {
        transition: transform 1s ease-in-out;
    }

    .parallax .e-carousel-item.e-next {
        animation: ParallaxIn 1s ease-in-out;
    }

    .parallax .e-carousel-item.e-prev {
        animation: ParallaxOut 1s ease-in-out;
    }

    @@keyframes ParallaxIn {
        from {
            opacity: 0;
            transform: scale(0) translateY(100%);
        }

        to {
            opacity: 1;
            transform: scale(1) translateY(0);
        }
    }

    @@keyframes ParallaxOut {
        from {
            opacity: 1;
            transform: scale(1) translateY(0);
        }

        to {
            opacity: 0;
            transform: scale(0) translateY(-100%);
        }
    }
</style>

Carousel custom animation

Intervals between slides

Using the items property, you can set different intervals for each item to transition between slides. The default interval is 5000 ms (5 seconds). The following example depicts the code for setting the different intervals between each item.

<div class="container">
    <div class="control-container">
        <ejs-carousel id="carousel">
            <e-carousel-items>
                <e-carousel-item template='<div class="slide-content">Slide 1</div>' interval="3000"></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 2</div>' interval="1000"></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 3</div>' interval="2000"></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 4</div>' interval="5000"></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 5</div>' interval="6000"></e-carousel-item>
            </e-carousel-items>
        </ejs-carousel>
    </div>
</div>
<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>

Note: Interval property can accept value in terms of milliseconds.

Auto play slides

In the carousel, all slides transitions are performed continuously after the specified or default intervals. You can enable or disable the auto slide transition using the autoPlay property. The following example depicts the code to disable the auto slide transitions.

<div class="container">
    <div class="control-container">
        <ejs-carousel id="carousel" autoPlay="false">
            <e-carousel-items>
                <e-carousel-item template='<div class="slide-content">Slide 1</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 2</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 3</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 4</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 5</div>'></e-carousel-item>
            </e-carousel-items>
        </ejs-carousel>
    </div>
</div>
<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>

Pause on hover

By default, Slide transitions are paused when hovering the mouse pointer over the Carousel element. You can enable or disable this functionality using thepauseOnHover property.

The following example depicts the code to play the slides when hovering the mouse pointer over the Carousel element.

@using Syncfusion.EJ2.Navigations;

<div class="container">
    <div class="control-container">
        @(Html.EJS().Carousel("defaultCarousel").PauseOnHover(false).Items(new List<CarouselItem> {
                    new CarouselItem { Template = "<div class='slide-content'>Slide 1</div>"  },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 2</div>" },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 3</div>" },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 4</div>"  },
                    new CarouselItem { Template = "<div class='slide-content'>Slide 5</div>"  }
                })
                .Render()
            )
    </div>
</div>

<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>

Looping slides

In the carousel, slides transitions are repeated continuously when you reach the last slide by default. You can enable or disable the infinite slide transition using the loop property. The following example depicts the code to disable the infinite slide transitions.

<div class="container">
    <div class="control-container">
        <ejs-carousel id="carousel" loop="false">
            <e-carousel-items>
                <e-carousel-item template='<div class="slide-content">Slide 1</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 2</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 3</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 4</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 5</div>'></e-carousel-item>
            </e-carousel-items>
        </ejs-carousel>
    </div>
</div>
<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>

Carousel loop slides

Slide changing events

Using the slideChanging or slideChanged events of the Carousel component, you can perform sample end customization while the carousel items are switched.

The following demo depicts the example for carousel events,

<div class="container">
    <div class="control-container">
        <ejs-carousel id="carousel" slideChanging="onSlideChanging" slideChanged="onSlideChanged">
            <e-carousel-items>
                <e-carousel-item template='<div class="slide-content">Slide 1</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 2</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 3</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 4</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 5</div>'></e-carousel-item>
            </e-carousel-items>
        </ejs-carousel>
    </div>
</div>

<script type="text/javascript">
    function onSlideChanging(args) {
        console.log(args.currentSlide); // You can customize the slide before changing
    }
    function onSlideChanged(args) {
        console.log(args.currentSlide); // You can customize the slide after changed
    }
</script>
<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>

Disable touch swiping

In the carousel, you can swipe the carousel slides using touch actions by default. The swipe action can be enabled or disabled using the enableTouchSwipe property. The following example depicts the code to disable the swipe action for the slide.

<div class="container">
    <div class="control-container">
        <ejs-carousel id="carousel" enableTouchSwipe="false">
            <e-carousel-items>
                <e-carousel-item template='<div class="slide-content">Slide 1</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 2</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 3</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 4</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 5</div>'></e-carousel-item>
            </e-carousel-items>
        </ejs-carousel>
    </div>
</div>
<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>

NOTE

View Sample in GitHub.

Swipe Modes

In the carousel, the swipeMode property allows specifying whether the slide transition should occur while performing swiping via touch or mouse. The slide swiping is enabled or disabled using the bitwise operator.

The following are the different swipe modes available in the carousel:

  • CarouselSwipeMode.Touch - Allows the user to slide the slides using touch actions.
  • CarouselSwipeMode.Mouse - Allows the user to slide the slides using mouse actions.
  • CarouselSwipeMode.Touch & CarouselSwipeMode.Mouse - Allows the user to slide the slides using both touch and mouse actions.
  • ~CarouselSwipeMode.Touch & ~CarouselSwipeMode.Mouse - Disables both touch and mouse actions.
<div class="container">
    <div class="control-container">
        <ejs-carousel id="carousel" autoPlay="false" swipeMode="Mouse & CarouselSwipeMode.Touch">
            <e-carousel-items>
                <e-carousel-item template='<div class="slide-content">Slide 1</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 2</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 3</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 4</div>'></e-carousel-item>
                <e-carousel-item template='<div class="slide-content">Slide 5</div>'></e-carousel-item>
            </e-carousel-items>
        </ejs-carousel>
    </div>
</div>
<style>
    .control-container {
        background-color: #adb5bd;
        height: 300px;
        margin: 0 auto;
        width: 500px;
    }

    .e-carousel .slide-content {
        align-items: center;
        display: flex;
        font-size: 1.25rem;
        height: 100%;
        justify-content: center;
    }
</style>

Carousel Swipe Mode