openaire-library/utils/full-page-slider/full-page-slider.component.ts

53 lines
1.3 KiB
TypeScript
Raw Normal View History

import {AfterContentInit, Component, ContentChildren, Input, OnInit, QueryList} from "@angular/core";
import {SlideComponent} from "./slide.component";
@Component({
selector: 'fp-slider',
template: `
<section (wheel)="onWheel($event)">
<ng-content></ng-content>
</section>`,
styleUrls: ['full-page-slider.component.css']
})
export class FullPageSliderComponent implements AfterContentInit {
@ContentChildren(SlideComponent) slides: QueryList<SlideComponent>;
@Input()
public initSlide = 1;
public animate: boolean = false;
public state = 0;
ngAfterContentInit() {
this.state = this.initSlide;
this.setSlides(this.state);
}
setSlides(state = 1) {
this.slides.forEach((slide, index) => {
slide.state = state;
slide.y = -50 + (index + 1)*200 - state*200;
});
}
onWheel(event) {
if (!this.animate) {
this.animate = true;
if (event.deltaY > 0 && (this.state < this.slides.length)) {
this.state++;
this.setSlides(this.state);
setTimeout(() => {
this.animate = false;
}, 1000);
} else if (event.deltaY < 0 && (this.state !== 1)) {
this.state--;
this.setSlides(this.state);
setTimeout(() => {
this.animate = false;
}, 1000);
} else {
this.animate = false;
}
}
}
}