Loading...
Loading...
Compare original and translation side by side
import { 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 {}import { 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>'None' | 'Slide' | 'Fade' | 'Custom''Slide'// 淡入淡出动画
<ejs-carousel [animationEffect]="'Fade'">
<!-- 幻灯片间平滑淡入淡出 -->
</ejs-carousel>
// 无动画
<ejs-carousel [animationEffect]="'None'">
<!-- 即时切换幻灯片 -->
</ejs-carousel>
// 自定义CSS动画
<ejs-carousel [animationEffect]="'Custom'" [cssClass]="'custom-animation'">
<!-- 在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>booleantrue<ejs-carousel [autoPlay]="true" [interval]="3000">
<!-- 每3秒自动切换 -->
</ejs-carousel>number5000<ejs-carousel [autoPlay]="true" [interval]="2000">
<!-- 幻灯片间隔2秒 -->
</ejs-carousel>booleantrue<ejs-carousel [autoPlay]="true" [pauseOnHover]="false">
<!-- 即使悬停也继续自动播放 -->
</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>'Visible' | 'Hidden' | 'VisibleOnHover''Visible'// 始终可见
<ejs-carousel [buttonsVisibility]="'Visible'">
<!-- 按钮始终显示 -->
</ejs-carousel>
// 仅在悬停时可见
<ejs-carousel [buttonsVisibility]="'VisibleOnHover'">
<!-- 鼠标悬停时显示按钮 -->
</ejs-carousel>
// 始终隐藏
<ejs-carousel [buttonsVisibility]="'Hidden'">
<!-- 使用指示器或滑动进行导航 -->
</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><!-- 轮播项 --></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><!-- 轮播项 --></e-carousel-items>
</ejs-carousel>booleantrue<ejs-carousel [loop]="true">
<!-- 最后一张幻灯片 → 第一张幻灯片 -->
</ejs-carousel>
<ejs-carousel [loop]="false">
<!-- 最后一张幻灯片 → 导航停止 -->
</ejs-carousel>number0<ejs-carousel [selectedIndex]="2">
<!-- 从第三张幻灯片开始(索引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>booleantrue<ejs-carousel [showIndicators]="true">
<!-- 指示器可见 -->
</ejs-carousel>
<ejs-carousel [showIndicators]="false">
<!-- 不显示指示器 -->
</ejs-carousel>'Default' | 'Dynamic' | 'Fraction' | 'Progress''Default'// 点指示器
<ejs-carousel [indicatorsType]="'Default'">
<!-- 显示:● ● ● ● ● -->
</ejs-carousel>
// 数字格式
<ejs-carousel [indicatorsType]="'Fraction'">
<!-- 显示:1 / 5 -->
</ejs-carousel>
// 进度条
<ejs-carousel [indicatorsType]="'Progress'">
<!-- 可视化进度条 -->
</ejs-carousel>
// 动画效果
<ejs-carousel [indicatorsType]="'Dynamic'">
<!-- 动画指示器效果 -->
</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><!-- 轮播项 --></e-carousel-items>
</ejs-carousel>booleantrue<ejs-carousel [autoPlay]="false" [showPlayButton]="true">
<!-- 用户可以点击播放恢复自动播放 -->
</ejs-carousel>
<ejs-carousel [showPlayButton]="false">
<!-- 不显示播放按钮 -->
</ejs-carousel>string | objectnull<ejs-carousel [playButtonTemplate]="'#playBtnTemplate'">
<ng-template #playBtnTemplate>
<button class="play-btn">▶ Resume</button>
</ng-template>
<e-carousel-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>any[]null// 组件
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' }
];
}
// 模板
<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'">
<!-- 通过CSS类应用自定义样式 -->
</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>booleantrue<ejs-carousel [enableTouchSwipe]="true">
<!-- 启用触摸滑动 -->
</ejs-carousel>
<ejs-carousel [enableTouchSwipe]="false">
<!-- 禁用触摸滑动 -->
</ejs-carousel>'Touch' | 'Mouse' | 'Touch|Mouse''Touch|Mouse'// 触摸和鼠标都支持
<ejs-carousel [swipeMode]="'Touch|Mouse'" [enableTouchSwipe]="true">
<!-- 两种交互方式都有效 -->
</ejs-carousel>
// 仅支持触摸
<ejs-carousel [swipeMode]="'Touch'" [enableTouchSwipe]="true">
<!-- 仅触摸手势有效 -->
</ejs-carousel>
// 仅支持鼠标
<ejs-carousel [swipeMode]="'Mouse'" [enableTouchSwipe]="true">
<!-- 仅鼠标拖动有效 -->
</ejs-carousel>booleantrue<ejs-carousel [allowKeyboardInteraction]="true">
<!-- 方向键可导航幻灯片 -->
</ejs-carousel>
<ejs-carousel [allowKeyboardInteraction]="false">
<!-- 禁用方向键导航 -->
</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 | number'100%'// 固定高度
<ejs-carousel [height]="400">
<!-- 高度400px -->
</ejs-carousel>
// 百分比高度
<ejs-carousel [height]="'100%'">
<!-- 占父容器全部高度 -->
</ejs-carousel>string | number'100%'// 固定宽度
<ejs-carousel [width]="800">
<!-- 宽度800px -->
</ejs-carousel>
// 全宽
<ejs-carousel [width]="'100%'">
<!-- 占父容器全部宽度 -->
</ejs-carousel>booleanfalse<ejs-carousel [partialVisible]="true">
<!-- 显示相邻幻灯片的部分内容 -->
</ejs-carousel>
<ejs-carousel [partialVisible]="false">
<!-- 仅当前幻灯片完全可见 -->
</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>string''<ejs-carousel [locale]="'de-DE'">
<!-- 德语本地化 -->
</ejs-carousel>
<ejs-carousel [locale]="'fr-FR'">
<!-- 法语本地化 -->
</ejs-carousel>booleanfalse<ejs-carousel [enablePersistence]="true">
<!-- 当前幻灯片位置会被保存并恢复 -->
</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>booleanfalse<ejs-carousel [enableRtl]="true">
<!-- 适用于阿拉伯语、希伯来语等的RTL布局 -->
</ejs-carousel>Record<string, any>null<ejs-carousel [htmlAttributes]="{ 'data-type': 'hero', 'aria-label': 'Hero Carousel' }">
<!-- 应用自定义属性 -->
</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();
}
}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();
}
}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();
}
}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();
}
}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();
}
}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() {
// 组件销毁时清理
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");
}
}
}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">无法导航到幻灯片2</div>
<ejs-carousel #carousel (slideChanging)="onSlideChanging($event)">
<e-carousel-items>
<e-carousel-item><ng-template #template><img src="img1.jpg" /><p>幻灯片1</p></ng-template></e-carousel-item>
<e-carousel-item><ng-template #template><img src="img2.jpg" /><p>幻灯片2 - 已阻止</p></ng-template></e-carousel-item>
<e-carousel-item><ng-template #template><img src="img3.jpg" /><p>幻灯片3</p></ng-template></e-carousel-item>
</e-carousel-items>
</ejs-carousel>
`
})
export class CarouselEventComponent {
onSlideChanging(args: SlideChangingEventArgs) {
console.log(`正在从${args.currentIndex}切换到${args.nextIndex}`);
// 阻止导航到幻灯片2
if (args.nextIndex === 1) {
args.cancel = true;
alert("幻灯片2已被阻止");
}
}
}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}`);
}
}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">第{{currentSlide}}张,共{{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(`当前幻灯片索引:${args.currentIndex}`);
console.log(`切换方向:${args.slideDirection}`);
console.log(`是否由滑动触发:${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 |
| 属性 | 默认值 | 类型 |
|---|---|---|
| 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, 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 } from "@syncfusion/ej2-angular-navigations";
@Component({
selector: "app-manual-nav",
template: `
<div class="carousel-controls">
<button (click)="prevSlide()" class="control-btn">← 上一页</button>
<span class="slide-counter">{{currentSlide}} / {{totalSlides}}</span>
<button (click)="nextSlide()" class="control-btn">下一页 →</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}`);
}
}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>总浏览次数:{{viewCount}}</p>
<p>当前浏览:{{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: '幻灯片1', image: 'image1.jpg' },
{ title: '幻灯片2', image: 'image2.jpg' },
{ title: '幻灯片3', image: 'image3.jpg' }
];
viewCount = 0;
lastViewedSlide = '';
onSlideChanging(args: SlideChangingEventArgs) {
// 示例:阻止返回第一张幻灯片
if (args.previousIndex === 1 && args.nextIndex === 0) {
console.log("阻止返回第一张幻灯片");
// args.cancel = true; // 取消注释以启用阻止
}
}
onSlideChanged(args: SlideChangedEventArgs) {
this.viewCount++;
this.lastViewedSlide = this.slides[args.currentIndex].title;
// 记录分析数据
console.log(`通过${args.slideDirection}方式浏览了幻灯片${args.currentIndex}`);
}
}