How to fix header on page scroll

Posted By : Himanshu Khurana | 25-Jun-2017

You need add some JS to do scroll events. The good way to do this is to set a new CSS class for the fixed position that will get assigned to the relevant div when scrolling goes past a certain point.

You can see the example below:

HTML :


 
 

 

CSS :

.header-fixed {
    position: fixed;
    top:0;
    left:0;
    width: 100%; 
}

 

Jquery :

$(window).scroll(function(){
  var header = $('.header'),
      scroll = $(window).scrollTop();

  if (scroll >= 100) header.addClass('header-fixed');
  else header.removeClass('header-fixed');
});

 

This is the best way for fix header on page scroll.

 

I hope this blog has been helpful to understand a header fix on page scroll.

 

Thanks

 

About Author

Author Image
Himanshu Khurana

Himanshu is an experienced Frontend developer with experience and capabilities to build compelling UI's for Web and Mobile Applications. Himanshu likes playing cricket and listening music.

Request for Proposal

Name is required

Comment is required

Sending message..