first commit

This commit is contained in:
Dhanraj
2024-09-22 10:21:52 +05:30
parent 9c6c3abc32
commit bd5119fc3c
697 changed files with 112184 additions and 10841 deletions

View File

@ -0,0 +1,40 @@
import { Directive, Input, EventEmitter, Inject, Output, ElementRef, HostListener } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Directive({
selector: '[appScrollspy]'
})
export class ScrollspyDirective {
@Input() public spiedTags = [];
@Output() public sectionChange = new EventEmitter<string>();
private currentSection: string;
// tslint:disable-next-line: variable-name
constructor(private _el: ElementRef, @Inject(DOCUMENT) private document: Document,) { }
@HostListener('window:scroll', ['$event'])
/**
* Window scroll method
*/
onScroll(event: any) {
let currentSection: string;
const children = this._el.nativeElement.querySelectorAll('section');
const scrollTop = this.document.documentElement.scrollTop;
const parentOffset = this.document.documentElement.offsetTop;
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < children.length; i++) {
const element = children[i];
if (this.spiedTags.some(spiedTag => spiedTag === element.tagName)) {
if ((element.offsetTop - parentOffset) <= scrollTop) {
currentSection = element.id;
}
}
}
if (currentSection !== this.currentSection) {
this.currentSection = currentSection;
this.sectionChange.emit(this.currentSection);
}
}
}