我正在尝试编写一个自定义的轮播组件。我在这里遇到两个问题。
首先-在单击按钮或通过组件时触发动画。
第二个-在动画触发器中传递参数。
我正在犯什么错误?
carousel.component.html
<div class="carousel-wrapper">
<ul class="carousel-inner" #carousel>
<li class="carousel-button" *ngIf="showControls">
<span (click)="change('previous')"><i class="fa fa-angle-left" style="font-size:24px"></i></span>
</li>
<li *ngFor="let item of items;" class="carousel-item">
<ng-container [@animateSlide]="{ value: triggerVal, params : { offset: offset } }" [ngTemplateOutlet]="item.tpl">
</ng-container>
</li>
<li class="carousel-button" *ngIf="showControls">
<span (click)="change('next')"><i class="fa fa-angle-right" style="font-size:24px"></i></span>
</li>
</ul>
</div>
carousel.component.ts
import { Input, ViewChild, Component, AfterViewInit, Directive, ContentChildren, ViewChildren, QueryList, TemplateRef, ElementRef } from '@angular/core';
import { trigger, style, group, transition, state, animate, keyframes, query, stagger, animation, useAnimation } from '@angular/animations';
@Directive({
selector: '[carouselItem]'
})
export class CarouselItemDirective {
constructor( public tpl : TemplateRef<any> ) { }
}
@Directive({
selector: '.carousel-item'
})
export class CarouselItemElement {
constructor( public element : ElementRef<any> ){ }
}
@Component({
selector: 'app-carousel',
templateUrl: './carousel.component.html',
styleUrls: ['./carousel.component.css'],
animations: [
trigger('animateSlide', [
transition('* => for', [
animate( '250ms ease-in', style({ transform: 'translateX(-{{ offset }}px)' }))
])
])
]
})
export class CarouselComponent implements AfterViewInit {
@ContentChildren( CarouselItemDirective ) items : QueryList<CarouselItemDirective>;
@ViewChildren( CarouselItemElement, { read: ElementRef } ) private itemsElements : QueryList<ElementRef>;
@ViewChild('carousel') private carousel : ElementRef;
// @Input() timing = '250ms ease-in';
@Input() showControls = true;
private itemWidth : number;
public offset = 0;
public currentSlide = 0;
public carouselWrapperStyle = {};
public triggerVal : boolean;
constructor() {
this.triggerVal = false;
}
ngAfterViewInit() {
// this.itemWidth = this.itemsElements.first.nativeElement.getBoundingClientRect().width;
this.itemWidth = 100;
}
change( state ) {
if( this.currentSlide + 1 === this.items.length ) return;
if( state == 'next' ){
this.currentSlide = ( this.currentSlide + 1 ) % this.items.length;
} else {
this.currentSlide = ( ( this.currentSlide - 1 ) + this.items.length ) % this.items.length;
}
this.offset = this.currentSlide * this.itemWidth;
console.log( this.offset );
this.triggerVal = !this.triggerVal;
}
}
指令的使用-
<app-carousel>
<ng-container *ngFor="let item of items;">
<ng-container *carouselItem>
<div>{{ item.favouriteLocation }}</div>
</ng-container>
</ng-container>
</app-carousel>
package.json
{
"name": "",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@agm/core": "^1.0.0-beta.3",
"@angular/animations": "6.0.7",
"@angular/cli": "^6.0.8",
"@angular/common": "6.0.7",
"@angular/compiler": "6.0.7",
"@angular/core": "6.0.7",
"@angular/forms": "6.0.7",
"@angular/http": "6.0.7",
"@angular/platform-browser": "6.0.7",
"@angular/platform-browser-dynamic": "6.0.7",
"@angular/router": "6.0.7",
"@ng-bootstrap/ng-bootstrap": "^2.0.0",
"angular-font-awesome": "^3.1.2",
"bootstrap": "^4.1.1",
"core-js": "^2.4.1",
"font-awesome": "^4.7.0",
"jquery": "^3.3.1",
"popper.js": "^1.14.3",
"rxjs": "^6.2.1",
"rxjs-compat": "^6.0.0-rc.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.6.8",
"@angular/compiler-cli": "6.0.7",
"@angular/language-service": "6.0.7",
"@types/googlemaps": "^3.30.11",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "2.7.2"
}
}