I am looking for a simple way to make a different header appear when the normal one is scrolled down. I have been looking for many solution but they're all involving long code..
So here is the solution I found but that isn't working since y doesn't implement itself..
Here is the TypeScript
import { Component, OnInit, HostListener } from '@angular/core';`
...
export class HeaderComponent implements OnInit{
y = window.scrollY;
myID = document.getElementById("header");
myID2 = document.getElementById("header2");
ngOnInit() {
this.myScrollFunc();
window.addEventListener("scroll", this.myScrollFunc);
}
myScrollFunc() {
if (this.y >= 150) {
this.myID2.className = "bottomMenu hide"
this.myID.className = "bottomMenu show"
} else {
this.myID2.className = "bottomMenu show"
this.myID.className = "bottomMenu hide"
}
};
}
The HTML
<div id="header" class="bottomMenu show"> ... </div>
<div id="header2" class="bottomMenu hide"> ... </div>
And the .css
.hide {
opacity: 0;
left: -100%;
}
.show {
opacity: 1;
left: 0;
}
This apparently is supposed to work and looks simple but it doesn't work for me..
EDIT : If you have a simpler way I am also open, the solution isn't mainly to correct this one.
EDIT 2 : Still no answer, if anyone have an idea, or if there is a simpler solution, like if (position !=0) { y = 1 } else { y = 0 } and i'd add y in some ngIf in the HTML... ?
EDIT 3 : I finally found a solution, here is my code if someone has the same problem :
HTML
<div id="header" *ngIf="scrolled == 0"> ... </div>
<div id="header2" *ngIf="scrolled == 1"> ... </div>
TypeScript
export class HeaderComponent implements OnInit{
scrolled = 0;
@HostListener('window:scroll', ['$event'])
onWindowScroll($event) {
const numb = window.scrollY;
if (numb >= 50){
this.scrolled = 1;
}
else {
this.scrolled = 0;
}
}
}
Easier than long listener
Thank you very much for your help in advance !