Loading...
Loading...
Learn how to implement the Syncfusion Angular Carousel component for displaying slides with images and content. This comprehensive skill includes complete API documentation with all 30 properties, 5 methods, 2 events, and working code examples. Use when creating image galleries, product showcases, featured content sliders, news rotators, or implementing carousel navigation with animations.
npx skill4agent add syncfusion/angular-ui-components-skills syncfusion-angular-carouselimport { Component } from "@angular/core";
import { CarouselModule } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-carousel",
standalone: true,
imports: [CarouselModule],
template: `
<div class="carousel-container">
<ejs-carousel [autoPlay]="true" [animationEffect]="'Slide'">
<e-carousel-items>
<e-carousel-item>
<ng-template #template>
<figure class="img-container">
<img src="https://ej2.syncfusion.com/products/images/carousel/cardinal.png" alt="Slide 1" />
<figcaption>Cardinal</figcaption>
</figure>
</ng-template>
</e-carousel-item>
<e-carousel-item>
<ng-template #template>
<figure class="img-container">
<img src="https://ej2.syncfusion.com/products/images/carousel/hunei.png" alt="Slide 2" />
<figcaption>Kingfisher</figcaption>
</figure>
</ng-template>
</e-carousel-item>
<e-carousel-item>
<ng-template #template>
<figure class="img-container">
<img src="https://ej2.syncfusion.com/products/images/carousel/costa-rica.png" alt="Slide 3" />
<figcaption>Keel-billed Toucan</figcaption>
</figure>
</ng-template>
</e-carousel-item>
</e-carousel-items>
</ejs-carousel>
</div>
`,
styles: [`
.carousel-container {
height: 400px;
margin: 20px 0;
}
`]
})
export class CarouselComponent {}'None' | 'Slide' | 'Fade' | 'Custom''Slide'// Fade animation
<ejs-carousel [animationEffect]="'Fade'">
<!-- Smooth fade in/out between slides -->
</ejs-carousel>
// No animation
<ejs-carousel [animationEffect]="'None'">
<!-- Instant slide transition -->
</ejs-carousel>
// Custom CSS animation
<ejs-carousel [animationEffect]="'Custom'" [cssClass]="'custom-animation'">
<!-- Define custom animation in CSS -->
</ejs-carousel>booleantrue<ejs-carousel [autoPlay]="true" [interval]="3000">
<!-- Auto transitions every 3 seconds -->
</ejs-carousel>number5000<ejs-carousel [autoPlay]="true" [interval]="2000">
<!-- 2 second interval between slides -->
</ejs-carousel>booleantrue<ejs-carousel [autoPlay]="true" [pauseOnHover]="false">
<!-- Continue auto-play even on hover -->
</ejs-carousel>'Visible' | 'Hidden' | 'VisibleOnHover''Visible'// Always visible
<ejs-carousel [buttonsVisibility]="'Visible'">
<!-- Buttons always shown -->
</ejs-carousel>
// Only on hover
<ejs-carousel [buttonsVisibility]="'VisibleOnHover'">
<!-- Buttons appear on mouse hover -->
</ejs-carousel>
// Never visible
<ejs-carousel [buttonsVisibility]="'Hidden'">
<!-- Use indicators or swipe instead -->
</ejs-carousel>string | objectnull<ejs-carousel [previousButtonTemplate]="'#prevBtnTemplate'">
<ng-template #prevBtnTemplate>
<button class="custom-prev"><i class="icon-left"></i> Back</button>
</ng-template>
<e-carousel-items><!-- Items --></e-carousel-items>
</ejs-carousel>string | objectnull<ejs-carousel [nextButtonTemplate]="'#nextBtnTemplate'">
<ng-template #nextBtnTemplate>
<button class="custom-next">Next <i class="icon-right"></i></button>
</ng-template>
<e-carousel-items><!-- Items --></e-carousel-items>
</ejs-carousel>booleantrue<ejs-carousel [loop]="true">
<!-- Last slide -> First slide -->
</ejs-carousel>
<ejs-carousel [loop]="false">
<!-- Last slide -> Navigation stops -->
</ejs-carousel>number0<ejs-carousel [selectedIndex]="2">
<!-- Start on third slide (index 2) -->
</ejs-carousel>booleantrue<ejs-carousel [showIndicators]="true">
<!-- Indicators visible -->
</ejs-carousel>
<ejs-carousel [showIndicators]="false">
<!-- No indicators shown -->
</ejs-carousel>'Default' | 'Dynamic' | 'Fraction' | 'Progress''Default'// Dot indicators
<ejs-carousel [indicatorsType]="'Default'">
<!-- Shows: ● ● ● ● ● -->
</ejs-carousel>
// Numeric format
<ejs-carousel [indicatorsType]="'Fraction'">
<!-- Shows: 1 / 5 -->
</ejs-carousel>
// Progress bar
<ejs-carousel [indicatorsType]="'Progress'">
<!-- Visual progress bar -->
</ejs-carousel>
// Animated
<ejs-carousel [indicatorsType]="'Dynamic'">
<!-- Animated indicator effect -->
</ejs-carousel>string | objectnull<ejs-carousel [indicatorsTemplate]="'#indicatorTemplate'" [showIndicators]="true">
<ng-template #indicatorTemplate let-data let-index="index">
<img [src]="items[index]?.thumbnail" class="thumb" />
</ng-template>
<e-carousel-items><!-- Items --></e-carousel-items>
</ejs-carousel>booleantrue<ejs-carousel [autoPlay]="false" [showPlayButton]="true">
<!-- Users can click play to resume auto-play -->
</ejs-carousel>
<ejs-carousel [showPlayButton]="false">
<!-- No play button visible -->
</ejs-carousel>string | objectnull<ejs-carousel [playButtonTemplate]="'#playBtnTemplate'">
<ng-template #playBtnTemplate>
<button class="play-btn">▶ Resume</button>
</ng-template>
<e-carousel-items><!-- Items --></e-carousel-items>
</ejs-carousel>any[]null// Component
export class CarouselComponent {
public slides = [
{ id: 1, image: 'img1.jpg', title: 'Slide 1' },
{ id: 2, image: 'img2.jpg', title: 'Slide 2' },
{ id: 3, image: 'img3.jpg', title: 'Slide 3' }
];
}
// Template
<ejs-carousel [dataSource]="slides">
<ng-template #itemTemplate let-item>
<figure>
<img [src]="item.image" [alt]="item.title" />
<figcaption>{{item.title}}</figcaption>
</figure>
</ng-template>
</ejs-carousel>string | objectnull<ejs-carousel [dataSource]="products">
<ng-template #itemTemplate let-product>
<div class="product-card">
<img [src]="product.image" />
<h3>{{product.name}}</h3>
<p>${{product.price}}</p>
</div>
</ng-template>
</ejs-carousel>CarouselItem[][]<ejs-carousel>
<e-carousel-items>
<e-carousel-item>
<ng-template #template><img src="image1.jpg" /></ng-template>
</e-carousel-item>
<e-carousel-item>
<ng-template #template><img src="image2.jpg" /></ng-template>
</e-carousel-item>
</e-carousel-items>
</ejs-carousel>string''<ejs-carousel [cssClass]="'dark-theme full-width'">
<!-- Apply custom styles via CSS classes -->
</ejs-carousel>booleantrue<ejs-carousel [enableTouchSwipe]="true">
<!-- Touch swipe enabled -->
</ejs-carousel>
<ejs-carousel [enableTouchSwipe]="false">
<!-- Touch swipe disabled -->
</ejs-carousel>'Touch' | 'Mouse' | 'Touch|Mouse''Touch|Mouse'// Both touch and mouse
<ejs-carousel [swipeMode]="'Touch|Mouse'" [enableTouchSwipe]="true">
<!-- Both interactions work -->
</ejs-carousel>
// Touch only
<ejs-carousel [swipeMode]="'Touch'" [enableTouchSwipe]="true">
<!-- Only touch gestures work -->
</ejs-carousel>
// Mouse only
<ejs-carousel [swipeMode]="'Mouse'" [enableTouchSwipe]="true">
<!-- Only mouse drag works -->
</ejs-carousel>booleantrue<ejs-carousel [allowKeyboardInteraction]="true">
<!-- Arrow keys navigate slides -->
</ejs-carousel>
<ejs-carousel [allowKeyboardInteraction]="false">
<!-- Arrow keys disabled -->
</ejs-carousel>string | number'100%'// Fixed height
<ejs-carousel [height]="400">
<!-- 400px height -->
</ejs-carousel>
// Percentage
<ejs-carousel [height]="'100%'">
<!-- Full parent height -->
</ejs-carousel>string | number'100%'// Fixed width
<ejs-carousel [width]="800">
<!-- 800px width -->
</ejs-carousel>
// Full width
<ejs-carousel [width]="'100%'">
<!-- Full parent width -->
</ejs-carousel>booleanfalse<ejs-carousel [partialVisible]="true">
<!-- Shows portion of adjacent slides -->
</ejs-carousel>
<ejs-carousel [partialVisible]="false">
<!-- Only current slide fully visible -->
</ejs-carousel>string''<ejs-carousel [locale]="'de-DE'">
<!-- German localization -->
</ejs-carousel>
<ejs-carousel [locale]="'fr-FR'">
<!-- French localization -->
</ejs-carousel>booleanfalse<ejs-carousel [enablePersistence]="true">
<!-- Current slide position saved and restored -->
</ejs-carousel>booleanfalse<ejs-carousel [enableRtl]="true">
<!-- RTL layout for Arabic, Hebrew, etc. -->
</ejs-carousel>Record<string, any>null<ejs-carousel [htmlAttributes]="{ 'data-type': 'hero', 'aria-label': 'Hero Carousel' }">
<!-- Custom attributes applied -->
</ejs-carousel>next(): voidimport { Component, ViewChild } from "@angular/core";
import { CarouselComponent } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-carousel-nav",
template: `
<button (click)="goNext()">Next</button>
<ejs-carousel #carousel>
<e-carousel-items>
<e-carousel-item><ng-template #template><img src="img1.jpg" /></ng-template></e-carousel-item>
<e-carousel-item><ng-template #template><img src="img2.jpg" /></ng-template></e-carousel-item>
</e-carousel-items>
</ejs-carousel>
`
})
export class CarouselNavComponent {
@ViewChild("carousel") carousel!: CarouselComponent;
goNext() {
this.carousel.next();
}
}prev(): voidexport class CarouselNavComponent {
@ViewChild("carousel") carousel!: CarouselComponent;
goPrev() {
this.carousel.prev();
}
}play(): voidimport { Component, ViewChild } from "@angular/core";
import { CarouselComponent } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-carousel-controls",
template: `
<button (click)="playSlides()">Play</button>
<button (click)="pauseSlides()">Pause</button>
<ejs-carousel #carousel [autoPlay]="false">
<e-carousel-items>
<e-carousel-item><ng-template #template><img src="img1.jpg" /></ng-template></e-carousel-item>
<e-carousel-item><ng-template #template><img src="img2.jpg" /></ng-template></e-carousel-item>
</e-carousel-items>
</ejs-carousel>
`
})
export class CarouselControlsComponent {
@ViewChild("carousel") carousel!: CarouselComponent;
playSlides() {
this.carousel.play();
}
pauseSlides() {
this.carousel.pause();
}
}pause(): voidexport class CarouselControlsComponent {
@ViewChild("carousel") carousel!: CarouselComponent;
pauseSlides() {
this.carousel.pause();
}
}destroy(): voidimport { Component, ViewChild, OnDestroy } from "@angular/core";
import { CarouselComponent } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-carousel-cleanup",
template: `
<ejs-carousel #carousel>
<e-carousel-items>
<e-carousel-item><ng-template #template><img src="img1.jpg" /></ng-template></e-carousel-item>
</e-carousel-items>
</ejs-carousel>
`
})
export class CarouselCleanupComponent implements OnDestroy {
@ViewChild("carousel") carousel!: CarouselComponent;
ngOnDestroy() {
// Cleanup when component destroyed
this.carousel.destroy();
}
}SlideChangingEventArgscancel: booleancurrentIndex: numbercurrentSlide: HTMLElementnextIndex: numbernextSlide: HTMLElementisSwiped: booleanslideDirection: 'Next' | 'Previous'name: stringimport { Component, ViewChild } from "@angular/core";
import { CarouselComponent, SlideChangingEventArgs } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-carousel-event",
template: `
<div class="status">Cannot navigate to slide 2</div>
<ejs-carousel #carousel (slideChanging)="onSlideChanging($event)">
<e-carousel-items>
<e-carousel-item><ng-template #template><img src="img1.jpg" /><p>Slide 1</p></ng-template></e-carousel-item>
<e-carousel-item><ng-template #template><img src="img2.jpg" /><p>Slide 2 - BLOCKED</p></ng-template></e-carousel-item>
<e-carousel-item><ng-template #template><img src="img3.jpg" /><p>Slide 3</p></ng-template></e-carousel-item>
</e-carousel-items>
</ejs-carousel>
`
})
export class CarouselEventComponent {
onSlideChanging(args: SlideChangingEventArgs) {
console.log(`Transitioning from ${args.currentIndex} to ${args.nextIndex}`);
// Prevent navigating to slide 2
if (args.nextIndex === 1) {
args.cancel = true;
alert("Slide 2 is blocked");
}
}
}SlideChangedEventArgscurrentIndex: numbercurrentSlide: HTMLElementpreviousIndex: numberpreviousSlide: HTMLElementisSwiped: booleanslideDirection: 'Next' | 'Previous'name: stringimport { Component, ViewChild } from "@angular/core";
import { CarouselComponent, SlideChangedEventArgs } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-carousel-tracking",
template: `
<div class="slide-counter">Slide {{currentSlide}} of {{totalSlides}}</div>
<ejs-carousel #carousel (slideChanged)="onSlideChanged($event)">
<e-carousel-items>
<e-carousel-item><ng-template #template><img src="img1.jpg" /></ng-template></e-carousel-item>
<e-carousel-item><ng-template #template><img src="img2.jpg" /></ng-template></e-carousel-item>
<e-carousel-item><ng-template #template><img src="img3.jpg" /></ng-template></e-carousel-item>
</e-carousel-items>
</ejs-carousel>
`
})
export class CarouselTrackingComponent {
currentSlide = 1;
totalSlides = 3;
onSlideChanged(args: SlideChangedEventArgs) {
this.currentSlide = args.currentIndex + 1;
console.log(`Now on slide ${args.currentIndex}`);
console.log(`Direction: ${args.slideDirection}`);
console.log(`Swiped: ${args.isSwiped}`);
}
}| Property | Default | Type |
|---|---|---|
| allowKeyboardInteraction | true | boolean |
| animationEffect | 'Slide' | string |
| autoPlay | true | boolean |
| buttonsVisibility | 'Visible' | string |
| cssClass | '' | string |
| dataSource | null | any[] |
| enablePersistence | false | boolean |
| enableRtl | false | boolean |
| enableTouchSwipe | true | boolean |
| height | '100%' | string/number |
| htmlAttributes | null | Record |
| indicatorsTemplate | null | string/object |
| indicatorsType | 'Default' | string |
| interval | 5000 | number (ms) |
| itemTemplate | null | string/object |
| items | [] | CarouselItem[] |
| locale | '' | string |
| loop | true | boolean |
| nextButtonTemplate | null | string/object |
| partialVisible | false | boolean |
| pauseOnHover | true | boolean |
| playButtonTemplate | null | string/object |
| previousButtonTemplate | null | string/object |
| selectedIndex | 0 | number |
| showIndicators | true | boolean |
| showPlayButton | true | boolean |
| swipeMode | 'Touch|Mouse' | string |
| width | '100%' | string/number |
import { Component, OnInit } from "@angular/core";
import { HttpClient } from "@angular/common/http";
@Component({
selector: "app-data-carousel",
template: `
<ejs-carousel [dataSource]="items" [selectedIndex]="0">
<ng-template #itemTemplate let-item>
<figure class="img-container">
<img [src]="item.image" [alt]="item.title" />
<figcaption>{{item.title}}</figcaption>
</figure>
</ng-template>
</ejs-carousel>
`
})
export class DataCarouselComponent implements OnInit {
items: any[] = [];
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get<any>('/api/carousel-items')
.subscribe(data => this.items = data);
}
}import { Component, ViewChild } from "@angular/core";
import { CarouselComponent } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-manual-nav",
template: `
<div class="carousel-controls">
<button (click)="prevSlide()" class="control-btn">← Previous</button>
<span class="slide-counter">{{currentSlide}} / {{totalSlides}}</span>
<button (click)="nextSlide()" class="control-btn">Next →</button>
</div>
<ejs-carousel
#carousel
[autoPlay]="false"
[buttonsVisibility]="'Hidden'"
(slideChanged)="onSlideChanged($event)"
>
<e-carousel-items>
<e-carousel-item *ngFor="let item of items">
<ng-template #template>
<img [src]="item.image" />
</ng-template>
</e-carousel-item>
</e-carousel-items>
</ejs-carousel>
`
})
export class ManualNavCarouselComponent {
@ViewChild("carousel") carousel!: CarouselComponent;
items = [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
];
currentSlide = 1;
totalSlides = this.items.length;
prevSlide() { this.carousel.prev(); }
nextSlide() { this.carousel.next(); }
onSlideChanged(event: any) {
this.currentSlide = event.currentIndex + 1;
}
}import { Component, ViewChild } from "@angular/core";
import { CarouselComponent, SlideChangingEventArgs, SlideChangedEventArgs } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-event-carousel",
template: `
<div class="carousel-stats">
<p>Total Views: {{viewCount}}</p>
<p>Current: {{lastViewedSlide}}</p>
</div>
<ejs-carousel
#carousel
(slideChanging)="onSlideChanging($event)"
(slideChanged)="onSlideChanged($event)"
>
<e-carousel-items>
<e-carousel-item *ngFor="let item of slides">
<ng-template #template>
<img [src]="item.image" />
<h3>{{item.title}}</h3>
</ng-template>
</e-carousel-item>
</e-carousel-items>
</ejs-carousel>
`
})
export class EventCarouselComponent {
@ViewChild("carousel") carousel!: CarouselComponent;
slides = [
{ title: 'Slide 1', image: 'image1.jpg' },
{ title: 'Slide 2', image: 'image2.jpg' },
{ title: 'Slide 3', image: 'image3.jpg' }
];
viewCount = 0;
lastViewedSlide = '';
onSlideChanging(args: SlideChangingEventArgs) {
// Example: Prevent returning to first slide
if (args.previousIndex === 1 && args.nextIndex === 0) {
console.log("Blocking return to first slide");
// args.cancel = true; // Uncomment to prevent
}
}
onSlideChanged(args: SlideChangedEventArgs) {
this.viewCount++;
this.lastViewedSlide = this.slides[args.currentIndex].title;
// Log analytics
console.log(`Viewed slide ${args.currentIndex} via ${args.slideDirection}`);
}
}