Carousel走马灯

走马灯,轮播图

代码演示

基本用法

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-carousel-basic',
  4. template: `
  5. <WingBlank>
  6. <Carousel
  7. [infinite]="true"
  8. [vertical]="false"
  9. [selectedIndex]="1"
  10. (beforeChange)="beforeChange($event)"
  11. (afterChange)="afterChange($event)"
  12. >
  13. <CarouselSlide *ngFor="let item of state.data">
  14. <div style="display: inline-block; width: 100%;" [ngStyle]="{ height: state.imgHeight }">
  15. <img
  16. src="https://zos.alipayobjects.com/rmsportal/{{ item }}.png"
  17. style=" pointer-events: none; width: 100%;"
  18. />
  19. </div>
  20. </CarouselSlide>
  21. </Carousel>
  22. </WingBlank>
  23. `
  24. })
  25. export class DemoCarouselBasicComponent {
  26. state = {
  27. data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
  28. imgHeight: '184px'
  29. };
  30. beforeChange(event) {
  31. console.log('slide ' + event.from + ' to ' + event.to);
  32. }
  33. afterChange(event) {
  34. console.log('slide to ' + event);
  35. }
  36. }

子元素数量变化

子元素数量变化。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-carousel-basic-dynamic',
  4. template: `
  5. <WingBlank>
  6. <div Button (onClick)="onClick1()">Click me to add child</div>
  7. <WhiteSpace></WhiteSpace>
  8. <Carousel [infinite]="true" (beforeChange)="beforeChange($event)" (afterChange)="afterChange($event)">
  9. <CarouselSlide *ngFor="let item of state.data; let i = index" [ngStyle]="{ height: state.imgHeight }">
  10. <div style="display: inline-block; width: 100%;" [ngStyle]="{ height: state.imgHeight }">
  11. <img
  12. src="https://zos.alipayobjects.com/rmsportal/{{ item }}.png"
  13. style="pointer-events: none; width: 100%;"
  14. />
  15. </div>
  16. </CarouselSlide>
  17. </Carousel>
  18. </WingBlank>
  19. `
  20. })
  21. export class DemoCarouselBasicDynamicComponent {
  22. state = {
  23. data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
  24. imgHeight: '184px'
  25. };
  26. beforeChange(event) {
  27. console.log('slide ' + event.from + ' to ' + event.to);
  28. }
  29. afterChange(event) {
  30. console.log('slide to ' + event);
  31. }
  32. onClick1() {
  33. this.state.data.push('AiyWuByWklrrUDlFignR');
  34. }
  35. }

竖向

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-carousel-vertical',
  4. template: `
  5. <WingBlank>
  6. <WhiteSpace></WhiteSpace>
  7. <Carousel
  8. class="my-carousel"
  9. [autoplay]="true"
  10. [infinite]="true"
  11. [vertical]="true"
  12. [dots]="false"
  13. [dragging]="false"
  14. >
  15. <CarouselSlide *ngFor="let item of state.data">
  16. <div class="v-item">carousel {{ item }}</div>
  17. </CarouselSlide>
  18. </Carousel>
  19. </WingBlank>
  20. `,
  21. styles: [
  22. `
  23. .my-carousel .v-item {
  24. height: 36px;
  25. line-height: 36px;
  26. padding-left: 10px;
  27. }
  28. `
  29. ]
  30. })
  31. export class DemoCarouselVerticalComponent {
  32. state = {
  33. data: ['1', '2', '3']
  34. };
  35. }

抽奖

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-carousel-lottery',
  4. template: `
  5. <WingBlank>
  6. <WhiteSpace></WhiteSpace>
  7. <Carousel
  8. class="my-carousel"
  9. [dots]="false"
  10. [speed]="200"
  11. [autoplay]="true"
  12. [infinite]="true"
  13. [vertical]="true"
  14. [dragging]="false"
  15. [autoplayInterval]="300"
  16. >
  17. <CarouselSlide *ngFor="let item of state.data; let i = index">
  18. <div class="v-item">carousel {{ item }}</div>
  19. </CarouselSlide>
  20. </Carousel>
  21. </WingBlank>
  22. `,
  23. styles: [
  24. `
  25. .my-carousel .v-item {
  26. height: 36px;
  27. line-height: 36px;
  28. padding-left: 10px;
  29. }
  30. `
  31. ]
  32. })
  33. export class DemoCarouselLotteryComponent {
  34. state = {
  35. data: ['ring', 'ruby', 'iPhone', 'iPod', 'sorry', 'tourism', 'coke', 'ticket', 'note']
  36. };
  37. }

Carousel走马灯 - 图1

API

参数说明类型默认值
[selectedIndex]手动设置当前显示的索引number0
[dots]是否显示面板指示点booleantrue
[vertical]垂直显示booleanfalse
[autoplay]是否自动切换booleanfalse
[autoplayInterval]自动切换的时间间隔number3000
[infinite]是否循环播放booleanfalse
[dotStyle]指示点样式object-
[dotActiveStyle]当前激活的指示点样式object-
[swipeSpeed]滑动灵敏度number12
[dragging]是否允许滑动切换boolean(子元素大于1时为true, 否则为false)
(afterChange)切换面板后的回调函数EventEmitter<number>-