Carousel走马灯

走马灯,轮播图

代码演示

基本用法

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-carousel-basic',
  4. template: `
  5. <WingBlank>
  6. <Carousel [infinite]="true"
  7. [vertical]="false"
  8. [selectedIndex]="1"
  9. (beforeChange)="beforeChange($event)"
  10. (afterChange)="afterChange($event)"
  11. >
  12. <CarouselSlide *ngFor="let item of state.data">
  13. <div style="display: inline-block; width: 100%;" [ngStyle]="{'height': state.imgHeight}">
  14. <img src="https://zos.alipayobjects.com/rmsportal/{{item}}.png" style=" pointer-events: none; width: 100%;"/>
  15. </div>
  16. </CarouselSlide>
  17. </Carousel>
  18. </WingBlank>
  19. `
  20. })
  21. export class DemoCarouselBasicComponent {
  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. }

子元素数量变化

子元素数量变化。

  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"
  9. (beforeChange)="beforeChange($event)"
  10. (afterChange)="afterChange($event)"
  11. >
  12. <CarouselSlide *ngFor="let item of state.data;let i = index" [ngStyle]="{'height': state.imgHeight}">
  13. <div style="display: inline-block; width: 100%;" [ngStyle]="{'height': state.imgHeight}">
  14. <img src="https://zos.alipayobjects.com/rmsportal/{{item}}.png" style="pointer-events: none; width: 100%;"/>
  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 class="my-carousel"
  8. [autoplay]="true"
  9. [infinite]="true"
  10. [vertical]="true"
  11. [dots]="false"
  12. [dragging]="false"
  13. >
  14. <CarouselSlide *ngFor="let item of state.data">
  15. <div class="v-item">carousel {{item}}</div>
  16. </CarouselSlide>
  17. </Carousel>
  18. </WingBlank>
  19. `,
  20. styles: [
  21. `
  22. .my-carousel .v-item {
  23. height: 36px;
  24. line-height: 36px;
  25. padding-left: 10px;
  26. }
  27. `
  28. ]
  29. })
  30. export class DemoCarouselVerticalComponent {
  31. state = {
  32. data: ['1', '2', '3']
  33. };
  34. }

抽奖

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

Carousel 走马灯 - 图1

API

属性说明类型默认值
selectedIndex手动设置当前显示的索引number0
dots是否显示面板指示点Booleantrue
vertical垂直显示Booleanfalse
autoplay是否自动切换Booleanfalse
autoplayInterval自动切换的时间间隔Number3000
infinite是否循环播放Booleanfalse
afterChange切换面板后的回调函数(current: number): void
dotStyle指示点样式Object
dotActiveStyle当前激活的指示点样式Object
swipeSpeed滑动灵敏度number12
dragging是否允许滑动切换Boolean(子元素大于1时为true, 否则为false)