Angular 11

Angular 11 Resizing Fixed Header After Scrolling

Angular 11 Resizing Fixed Header After Scrolling

Hello friends, welcome back to my blog. Today in this blog post, I am going to tell you, Angular 11 Resizing Fixed Header After Scrolling.


Angular 11 Resizing Fixed Header After Scrolling
Fixed Header Before Scroll
Angular Fixed Header After Scroll
Fixed Header After Scroll

Angular 11 came and if you are new then you must check below two links:

  1. Angular11 Basic Tutorials

Friends now I proceed onwards and here is the working code snippet for Angular 11 Resizing Fixed Header After Scrolling and please use carefully this to avoid the mistakes:

1. Firstly friends we need fresh angular 11 setup and for this we need to run below commands but if you already have angular 11 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:

npm install -g @angular/cli 

ng new angularfixedheader //Create new Angular Project

cd angularfixedheader // Go inside the Angular Project Folder

ng serve --open // Run and Open the Angular Project

http://localhost:4200/ // Working Angular Project Url

2. Now friends we just need to add below code into src/app/app.component.html file to get final out on the web browser:

<div class="header">
    <h1 class="header_logo">Angular 11 Resizing Fixed Header</h1>
  </div>
  <div class="offset">
    <p>Scroll down to initiate resizing of the page header.</p>
  </div>

3. Now friends we just need to css code code into app/app.component.css file:

.offset {
  margin-top: 0;
  padding-top: 270px;
  text-align: center;
  transition: .3s;
  height: 1500px;
}

.header {
  position: fixed;
  width: 100%;
  height: 250px;
  font-weight: bold;
  text-align: center;
  background: gold;
  transition: .3s;
}
.header.small {
  height: 70px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}
.header.small ~ .offset {
  padding-top: 140px;
}
.header.small .header_logo {
  padding-top: 20px;
  font-size: 20px;
  text-shadow: none;
}

.header_logo {
  font-family: 'Oswald', sans-serif;
  margin: 0;
  padding-top: 80px;
  font-size: 50px;
  text-shadow: 3px 4px rgba(0, 0, 0, 0.1);
  transition: .3s;
}

 

4. Now friends we just need to css code code into app/app.component.ts file:

...
export class AppComponent {
 ...
  ngOnInit()
  {
    window.addEventListener('scroll', function() {
      navbarScroll();
     });
     function navbarScroll() {
       var y = window.scrollY;
       if (y > 10) {
         var header = document.getElementsByClassName('header')[0];
           header.classList.add('small');
     
       } else if (y < 10) {
           var header = document.getElementsByClassName('header')[0];
          header.classList.remove('small');
       }
     }

  }
}

 

Friends in the end must run ng serve command into your terminal to run the angular 11 project.

Now we are done friends. If you have any kind of query, suggestion and new requirement then feel free to comment below.

Note: Friends, In this post, I just tell the basic setup and things, you can change the code according to your requirements.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad because with your views, I will make my next posts more good and helpful.

Jassa

Thanks

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, Angular 8, Angular 9, Angular 10, Angular 11, Nodejs, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.