Animated Header On Scroll with jQuery That Shrinks

Category: Navigation | May 31, 2019
Author: malZiiirA
Official Page: Go to Website
Last Update: September 1, 2019
License: MIT

Today, I have developed a small script to create an animated header on scroll with jQuery. To make it more interesting, I will add some text to the header which will resize when the user scrolls down the page.

It is a lightweight solution which allows you to customize according to your wishes. You can easily place navigation links and add logo on the header according to your web design need.

The header provides smooth and nice animation while scrolling the page. Such header perfects if you are working on one-page site.

You can also use this animated header if you want to build a stick header with navbar because It shrinks and stays at the top of the page when scrolling down.

The solution is ready for production sites but its work with the browsers that supports the most recent CSS3 features.

How to Create Animating Header On Scroll with jQuery

To create and quickly implement the solution on your site, you can simply load the both CSS and Jquery file into your webpage.

<script src=""></script>
<link rel="stylesheet" href="style.css">

Next, you need to add the markup. To create the markup we simply follow standing div with a class header. To add the content for the rest of the page, We define a div with class content.

<div class="header">
  <h1>Animated Fixed Header (Scroll Down)</h1>
<div class="content">
	Rest of Content Goes Here

The Style

Now we will play with Style. To make the header fixed at the top of the page, We will simply use the position fixed with top and left value zero to make it proper align.

We want to header take all over the place that’s why we use the width 100%. We did also apply a nice background color with font color white to make the text look nice.

The height 200px is just according to your choice. You can change it. For animation, We take help of transition element.

.header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: #cc5350;
    z-index: 1000;
    height: 200px;
    overflow: hidden;
    -webkit-transition: height 0.3s;
    -moz-transition: height 0.3s;
    transition: height 0.3s;


Now we will change the behavior of the animated header when it’s scroll down. What we do it to change the height to make slim and smart when the user scrolls down the page.

We will also change the line-height and font-size. You are noticing a shrink class and this will be handled through Javascript.

.header.shrink {
    height: 100px;
.header h1
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    transition: all 0.3s;
.header.shrink h1
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    transition: all 0.3s;

Finally, we have a content area and we setting height 500 just only because you can see the demo work perfectly. You don’t need that and remove the height while implementing on your live site.

.content {
    height: 500px;
    margin-top: 250px;
    text-align: center;
    font-size: 34px;
    line-height: 400px;

The Javascript

Last step to initialize the javascript function with the help of document. ready function. We will define the shrink header value 300 and tell the window browser by using the scroll function.

We will simply use the if/else statement to add or remove the class shrink. When the user reaches the browser window value 300, The script will add the shrink class next to header class else it will remove it.

$( document ).ready(function() {
 var shrinkHeader = 300;
  $(window).scroll(function() {
	var scroll = getCurrentScroll();
	  if ( scroll >= shrinkHeader ) {
		else {
function getCurrentScroll() {
	return window.pageYOffset || document.documentElement.scrollTop;

That’s it. It’s simple and easy to use solution to make the header sticky and animated.

You Might Be Interested In:

Leave a Comment

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