0
I’m trying to make the navbar have a "Fixed-top" class when moving the scroll and removing the class when it’s at the top of the page.
I wrote the script below but it’s not working.
import { Component, OnInit, AfterContentInit } from '@angular/core';
@Component({
selector: 'cb-header',
templateUrl: './header.component.html'
})
export class HeaderComponent implements OnInit {
constructor() { }
ngOnInit() {
}
ngAfterContentInit() {
() => {
let nav = document.getElementById('#nav');
window.addEventListener('scroll', () => {
if (window.scrollY > 1.5) {
nav.classList.add("fixed-top");
document.body.style.paddingTop = '70';
}else {
nav.classList.remove("fixed-top");
document.body.style.paddingTop = '0';
}
});
}
}
}
I had managed to resolve in a way that I believe is not the correct angular. I wrote the code straight into the component’s Aftercontentini. The code looks like this: (() => { const Nav = Document.getElementById('Nav'); window.addeventlistener('scroll', () => { if (window.Scrolly > 1.5) { Nav.classList.add("Fixed-top"); Document.body.style.paddingTop = "90px"; } Else { Nav.classList.remove("Fixed-top"); Document.body.style.paddingTop = "0px"; } }); })() }
– felipenoka
So you are working directly on the DOM, with Elementref and Viewchild you access the reference of the component and the template.
– lucasferreiralimax