我正在研究旋转木马的工作。但是我没有幻灯片,而是收到图片列表。
This example
slide.component.ts
import {Component, OnInit, OnDestroy, Input, HostBinding } from '@angular/core';
import {Carousel, Direction} from './carousel.component';
@Component({
selector: 'slide',
template: `
<div [class.active]="active" class="item text-center">
<ng-content></ng-content>
</div>
`
})
export class Slide implements OnInit, OnDestroy {
@Input() public index:number;
@Input() public direction:Direction;
@HostBinding('class.active')
@Input() public active:boolean;
@HostBinding('class.item')
@HostBinding('class.carousel-item')
private addClass:boolean = true;
constructor(private carousel:Carousel) {
}
public ngOnInit() {
this.carousel.addSlide(this);
}
public ngOnDestroy() {
this.carousel.removeSlide(this);
}
}
carousel.component.ts
import {Component, OnDestroy, Input} from '@angular/core';
import {Slide} from './slide.component';
export enum Direction {UNKNOWN, NEXT, PREV}
@Component({
selector: 'carousel',
template: `
<div (mouseenter)="pause()" (mouseleave)="play()" class="carousel slide">
<ol class="carousel-indicators" [hidden]="slides.length <= 1">
<li *ngFor="let slidez of slides" [class.active]="slidez.active === true" (click)="select(slidez)"></li>
</ol>
<div class="carousel-inner"><ng-content></ng-content></div>
<a class="left carousel-control" (click)="prev()" [hidden]="!slides.length">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" (click)="next()" [hidden]="!slides.length">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
`
})
export class Carousel implements OnDestroy {
@Input() public noWrap:boolean;
@Input() public noPause:boolean;
@Input() public noTransition:boolean;
@Input() public get interval():number {
return this._interval;
}
public set interval(value:number) {
this._interval = value;
this.restartTimer();
}
private slides:Array<Slide> = [];
private currentInterval:any;
private isPlaying:boolean;
private destroyed:boolean = false;
private currentSlide:Slide;
private _interval:number;
public ngOnDestroy() {
this.destroyed = true;
}
public select(nextSlide:Slide, direction:Direction = Direction.UNKNOWN) {
let nextIndex = nextSlide.index;
if (direction === Direction.UNKNOWN) {
direction = nextIndex > this.getCurrentIndex() ? Direction.NEXT : Direction.PREV;
}
// Prevent this user-triggered transition from occurring if there is already one in progress
if (nextSlide && nextSlide !== this.currentSlide) {
this.goNext(nextSlide, direction);
}
}
private goNext(slide:Slide, direction:Direction) {
if (this.destroyed) {
return;
}
slide.direction = direction;
slide.active = true;
if (this.currentSlide) {
this.currentSlide.direction = direction;
this.currentSlide.active = false;
}
this.currentSlide = slide;
// every time you change slides, reset the timer
this.restartTimer();
}
private getSlideByIndex(index:number) {
let len = this.slides.length;
for (let i = 0; i < len; ++i) {
if (this.slides[i].index === index) {
return this.slides[i];
}
}
}
private getCurrentIndex() {
return !this.currentSlide ? 0 : this.currentSlide.index;
}
private next() {
let newIndex = (this.getCurrentIndex() + 1) % this.slides.length;
if (newIndex === 0 && this.noWrap) {
this.pause();
return;
}
return this.select(this.getSlideByIndex(newIndex), Direction.NEXT);
}
private prev() {
let newIndex = this.getCurrentIndex() - 1 < 0 ? this.slides.length - 1 : this.getCurrentIndex() - 1;
if (this.noWrap && newIndex === this.slides.length - 1) {
this.pause();
return;
}
return this.select(this.getSlideByIndex(newIndex), Direction.PREV);
}
private restartTimer() {
this.resetTimer();
let interval = +this.interval;
if (!isNaN(interval) && interval > 0) {
this.currentInterval = setInterval(() => {
let nInterval = +this.interval;
if (this.isPlaying && !isNaN(this.interval) && nInterval > 0 && this.slides.length) {
this.next();
} else {
this.pause();
}
}, interval);
}
}
private resetTimer() {
if (this.currentInterval) {
clearInterval(this.currentInterval);
this.currentInterval = null;
}
}
public play() {
if (!this.isPlaying) {
this.isPlaying = true;
this.restartTimer();
}
}
public pause() {
if (!this.noPause) {
this.isPlaying = false;
this.resetTimer();
}
}
public addSlide(slide:Slide) {
slide.index = this.slides.length;
this.slides.push(slide);
if (this.slides.length === 1 || slide.active) {
this.select(this.slides[this.slides.length - 1]);
if (this.slides.length === 1) {
this.play();
}
} else {
slide.active = false;
}
}
public removeSlide(slide:Slide) {
this.slides.splice(slide.index, 1);
if (this.slides.length === 0) {
this.currentSlide = null;
return;
}
for (let i = 0; i < this.slides.length; i++) {
this.slides[i].index = i;
}
}
}
和使用的代码
import {Component} from '@angular/core';
import {Slide} from './slide.component';
import {Carousel} from './carousel.component';
/*Angular 2 Carousel - Gallery*/
@Component({
selector: 'my-app',
template:`
<div class="row">
<div class="col-md-12">
<carousel [interval]="NextPhotoInterval" [noWrap]="noLoopSlides">
<slide *ngFor=" let slidez of slides; let i=index"
[active]="slidez.active">
<img [src]="slidez.image" style="margin:auto;">
<div class="carousel-caption">
<h3 style="background-color: transparent;color: white;">Slide {{i + 1}}</h3>
<p style="background-color: transparent;color: white;">{{slidez.text}}</p>
</div>
</slide>
</carousel>
</div>
</div>
`,
})
export class Angular2Carousel {
//The time to show the next photo
private NextPhotoInterval:number = 5000;
//Looping or not
private noLoopSlides:boolean = true;
//Photos
private slides:Array<any> = [];
constructor() {
this.addNewSlide();
}
private addNewSlide() {
this.slides.push(
{image:'http://www.angulartypescript.com/wp-content/uploads/2016/03/car1.jpg',text:'BMW_1'},
{image:'http://www.angulartypescript.com/wp-content/uploads/2016/03/car2.jpg',text:'BMW_2'},
{image:'http://www.angulartypescript.com/wp-content/uploads/2016/03/car3.jpg',text:'BMW_3'},
{image:'http://www.angulartypescript.com/wp-content/uploads/2016/03/car4.jpg',text:'BMW_4'},
{image:'http://www.angulartypescript.com/wp-content/uploads/2016/03/car5.jpg',text:'BMW_5'},
{image:'http://www.angulartypescript.com/wp-content/uploads/2016/03/car6.jpg',text:'BMW_6'}
);
}
private removeLastSlide() {
this.slides.pop();
}
}
你能解释一下这个结果吗? enter image description here