In Angular application I am trying to shrink header component of Sticky header when user scrolls down. Currently sticky header works fine.
What changes do I need to make to display lower half of header with no text when user scroll down?
Actual component
Expected Output when user scrolls down
– Make Text disappear and display header as sticky showing lower 75% of component
This is what I have tried so far which makes whole component sticky when user scroll down
html
JavaScript
x
18
18
1
<div class="header-comp" [class.sticky] = "sticky" #stickyMenu>
2
<p class="text">Header</p>
3
<div class="placeholder">
4
//placeholder
5
</div>
6
7
</div>
8
9
<div class="dropdowns">
10
//dropdown
11
</div>
12
13
<div class="btn"> Button </button>
14
</div>
15
16
</div>
17
18
.scss
JavaScript
1
33
33
1
.header-comp {
2
width: 100%;
3
height: 148px;
4
background: grey;
5
border-radius: 0px;
6
}
7
8
.sticky{
9
position: fixed;
10
top: 0;
11
overflow: hidden;
12
z-index: 10;
13
transition: 0.90s;
14
}
15
16
17
.content{
18
height: 300vh;
19
}
20
21
.text {
22
width: 390px;
23
height: 26px;
24
color: tf-colors.$white;
25
font-size: 21px;
26
font-family: Arial;
27
letter-spacing: 0px;
28
line-height: 26px;
29
margin-left: 98px;
30
padding-top: 40px;
31
}
32
33
.ts
JavaScript
1
20
20
1
sticky: boolean = false;
2
@ViewChild('stickyMenu') menuElement: ElementRef;
3
4
menuPosition: any;
5
ngAfterViewInit(){
6
this.menuPosition = this.menuElement.nativeElement.offsetTop
7
console.log('mE',this.menuElement)
8
}
9
10
@HostListener('window:scroll', ['$event'])
11
handleScroll(){
12
const windowScroll = window.pageYOffset;
13
console.log(windowScroll)
14
if(windowScroll >= this.menuPosition){
15
this.sticky = true;
16
} else {
17
this.sticky = false;
18
}
19
}
20
Advertisement
Answer
For container to actually stick to the ceiling of the page we need to give negative top value.
JavaScript
1
8
1
.sticky{
2
position: fixed;
3
top: -60px; --------> Change top value to negative
4
overflow: hidden;
5
z-index: 10;
6
transition: 0.90s;
7
}
8