2022-02-21 15:33:02 +01:00
|
|
|
import {AfterViewInit, Component, Input, ViewEncapsulation} from "@angular/core";
|
2022-01-13 17:14:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-28 10:01:22 +01:00
|
|
|
* <section-scroll [customClass]="'optional classes for section e.g. uk-section-primary'"
|
|
|
|
* [childrenCustomClass]="'optional classes for content e.g. uk-container'">
|
|
|
|
* <div top>...</div>
|
|
|
|
* <div left>...</div>
|
|
|
|
* <div scroll>...</div>
|
2022-01-13 17:14:09 +01:00
|
|
|
* </section-scroll>
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
@Component({
|
|
|
|
selector: 'section-scroll',
|
|
|
|
template: `
|
2022-02-16 11:01:41 +01:00
|
|
|
<div [ngClass]="customClass">
|
2022-01-28 10:01:22 +01:00
|
|
|
<ng-content select="[top]"></ng-content>
|
2022-02-16 11:01:41 +01:00
|
|
|
<div [ngClass]="childrenCustomClass">
|
|
|
|
<div class="uk-grid" uk-grid>
|
2022-01-28 10:01:22 +01:00
|
|
|
<ng-content select="[left]"></ng-content>
|
|
|
|
<ng-content select="[scroll]"></ng-content>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-01-13 17:14:09 +01:00
|
|
|
`,
|
|
|
|
styleUrls: ['section-scroll.component.css'],
|
|
|
|
encapsulation: ViewEncapsulation.None
|
|
|
|
})
|
2022-02-21 15:33:02 +01:00
|
|
|
export class SectionScrollComponent implements AfterViewInit {
|
2022-01-13 17:14:09 +01:00
|
|
|
|
|
|
|
@Input()
|
|
|
|
public customClass = null;
|
2022-01-19 23:47:29 +01:00
|
|
|
@Input()
|
|
|
|
public childrenCustomClass = null;
|
2022-02-21 15:33:02 +01:00
|
|
|
@Input()
|
|
|
|
public animationSteps: number = 20;
|
2022-01-28 10:01:22 +01:00
|
|
|
private observer: IntersectionObserver = null;
|
2022-02-21 15:33:02 +01:00
|
|
|
/**
|
|
|
|
* Define as keys the ids of elements in scroll section and as values the ids of left section
|
|
|
|
* */
|
|
|
|
@Input()
|
|
|
|
private map: Map<string, string> = null;
|
|
|
|
private left: Map<string, HTMLElement> = new Map<string, HTMLElement>();
|
|
|
|
private scroll: Map<string, HTMLElement> = new Map<string, HTMLElement>();
|
|
|
|
|
|
|
|
constructor() {
|
2022-01-13 17:14:09 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 10:01:22 +01:00
|
|
|
ngOnDestroy() {
|
2022-02-21 15:33:02 +01:00
|
|
|
if (this.observer) {
|
2022-01-28 10:01:22 +01:00
|
|
|
this.observer.disconnect();
|
|
|
|
}
|
|
|
|
}
|
2022-01-13 17:14:09 +01:00
|
|
|
|
2022-01-28 10:01:22 +01:00
|
|
|
ngAfterViewInit() {
|
2022-02-21 15:33:02 +01:00
|
|
|
if (typeof document !== "undefined" && this.map) {
|
2022-01-28 10:01:22 +01:00
|
|
|
this.createObserver();
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 15:33:02 +01:00
|
|
|
|
2022-01-28 10:01:22 +01:00
|
|
|
createObserver() {
|
|
|
|
let observer;
|
|
|
|
let options = {
|
|
|
|
root: null,
|
|
|
|
rootMargin: "-20%",
|
|
|
|
threshold: this.buildThresholdList()
|
|
|
|
};
|
2022-02-21 15:33:02 +01:00
|
|
|
Array.from(this.map.values()).forEach(value => {
|
2022-02-22 23:37:18 +01:00
|
|
|
let element = document.getElementById(value);
|
2022-03-02 11:27:17 +01:00
|
|
|
element.style.display = 'none';
|
2022-02-22 23:37:18 +01:00
|
|
|
this.left.set(value, element);
|
2022-02-21 15:33:02 +01:00
|
|
|
})
|
|
|
|
Array.from(this.map.keys()).forEach(key => {
|
|
|
|
this.scroll.set(key, document.getElementById(key));
|
|
|
|
})
|
2022-01-28 10:01:22 +01:00
|
|
|
observer = new IntersectionObserver(entries => {
|
|
|
|
entries.forEach((entry) => {
|
2022-02-21 15:33:02 +01:00
|
|
|
entry.target['style'].opacity = String(entry.intersectionRatio);
|
|
|
|
let id = this.map.get(entry.target.id);
|
2022-03-02 11:27:17 +01:00
|
|
|
if(entry.isIntersecting) {
|
|
|
|
Array.from(this.left.keys()).forEach(element => {
|
|
|
|
if (element !== id) {
|
|
|
|
this.left.get(element).style.display = 'none';
|
|
|
|
} else {
|
|
|
|
this.left.get(element).style.display = 'block';
|
|
|
|
}
|
|
|
|
});
|
2022-01-28 10:01:22 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, options);
|
2022-02-21 15:33:02 +01:00
|
|
|
Array.from(this.scroll.values()).forEach(target => {
|
2022-01-28 10:01:22 +01:00
|
|
|
observer.observe(target)
|
|
|
|
});
|
|
|
|
}
|
2022-02-21 15:33:02 +01:00
|
|
|
|
2022-01-28 10:01:22 +01:00
|
|
|
buildThresholdList() {
|
|
|
|
let thresholds = [];
|
2022-02-21 15:33:02 +01:00
|
|
|
for (let i = 1.0; i <= this.animationSteps; i++) {
|
|
|
|
let ratio = i / this.animationSteps;
|
2022-01-28 10:01:22 +01:00
|
|
|
thresholds.push(ratio);
|
|
|
|
}
|
|
|
|
thresholds.push(0);
|
|
|
|
return thresholds;
|
|
|
|
}
|
2022-01-13 17:14:09 +01:00
|
|
|
}
|