Advertisement
  1. Web Design
  2. HTML/CSS
  3. HTML

JavaScript Skills: Create a Responsive Header Animation on Scroll

Scroll to top

In this tutorial, we’ll learn how to animate header elements on scroll. First, we’ll build a fully responsive top navigation header with three different layouts: one for small screens, one for medium screens, and one for large screens and above. 

Next, we’ll smoothly animate its call-to-action button on medium screens and above after a certain amount of scrolling. Sound interesting enough for joining me on the journey?

What We’ll be Building

Here’s a demo video which shows the behavior of our page header:

Here’s the corresponding Codepen demo (check out the larger version to see how the layout changes):

Let’s get started!

1. Begin With the Page Markup

Our page will consist of a header and two helper sections. Inside the header, we’ll place a navigation bar. This will include:

  • An image logo.
  • The main menu. Its last three items will be visible only on small screens.
  • The secondary menu. This will appear on screens greater than 767px. On smaller screens, its items will be part of the main menu.
  • A button responsible for toggling the mobile menu. This will be visible on screens up to 1100 pixels.

Here’s the markup:

1
<header class="page-header">
2
  <nav>
3
    <a href="">
4
      <img src="IMG_SRC" alt="">
5
    </a>
6
    <ul class="main-menu">
7
      <li>
8
        <a href="">Work</a>
9
      </li>
10
      <li>
11
        <a href="">About</a>
12
      </li>
13
      <li>
14
        <a href="">Clients</a>
15
      </li>
16
      <li>
17
        <a href="">News</a>
18
      </li>
19
      <li>
20
        <a href="">Login</a>
21
      </li>
22
      <li>
23
        <a href="">Pricing</a>
24
      </li>
25
      <li class="btn-wrapper">
26
        <a href="" class="btn">Sign Up</a>
27
      </li>
28
    </ul>
29
    <ul class="secondary-menu">
30
      <li>
31
        <a href="">Login</a>
32
      </li>
33
      <li>
34
        <a href="">Pricing</a>
35
      </li>
36
      <li>
37
        <a href="" class="btn">Sign Up</a>
38
      </li>
39
    </ul>
40
    <button class="toggle-mobile-menu" aria-label="Open Mobile Menu" type="button">
41
      <svg width="40" height="40" viewBox="0 0 24 24" fill="none" class="open-menu" aria-hidden="true">...</svg>
42
      <svg width="40" height="40" viewBox="0 0 24 24" fill="none" class="close-menu" aria-hidden="true">...</svg>
43
    </button>
44
  </nav>
45
</header>
46
<section class="hero">...</section>
47
<section class="main-content">...</section>

Beyond the header, we’ll create two sections with dummy content for testing the scrolling effect. For the shake of simplicity, for these elements, we won’t discuss their styles.

Note #1: To avoid creating duplicated content, instead of appending via HTML the last three items of the main menu, we could have dynamically added them via JavaScript. Remember that these are initially part of the secondary menu. 

Note #2: For this tutorial, I won’t cover how to make the mobile menu fully accessible. I’ve just used the aria-label attribute whose value will be updated via JavaScript and the aria-hidden attribute.

2. Define Some Basic Styles

With the markup ready, we’ll define some basic CSS styles. These will include a Google Font, a few custom variables, and some reset rules:

1
:root {
2
  --white: white;
3
  --deeppurple: #7c2a8a;
4
}
5
6
* {
7
  padding: 0;
8
  margin: 0;
9
  box-sizing: border-box;
10
}
11
12
ul {
13
  list-style: none;
14
}
15
16
button {
17
  background: none;
18
  border: none;
19
  outline: none;
20
  cursor: pointer;
21
}
22
23
a {
24
  text-decoration: none;
25
  color: inherit;
26
}
27
28
::-webkit-scrollbar {
29
  width: 10px;
30
}
31
32
::-webkit-scrollbar-track {
33
  box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.35);
34
}
35
36
::-webkit-scrollbar-thumb {
37
  background: var(--deeppurple);
38
}
39
40
body {
41
  font: 20px/1.5 "Inter", sans-serif;
42
}

Within the styles above, just for fun, we added some styles for customizing the default scrollbar styling. 

Customizing the default scrollbar styling

Keep in mind that not all browsers (e.g. Firefox 80) will adopt this new appearance.

Additionally, we’ll specify three helper classes for controlling the visibility of our elements. We’ll use them later when we toggle the mobile menu through JavaScript. Their names are inspired by Bootstrap 4’s class names:

1
.d-block {
2
  display: block !important;
3
}
4
5
.d-flex {
6
  display: flex !important;
7
}
8
9
.d-none {
10
  display: none !important;
11
}

Notice that all include the important property. As a general rule we should avoid using this property because it messes the styles and makes debugging difficult. In our example though, we’ll use it to apply styles through JavaScript to elements with different levels of specificity. 

3. Set the Header Styles

To build the header layout, we’ll follow a desktop-first approach.

Large Screens

On large screens (>1100px), its layout should look like this:

The header layout on large screensThe header layout on large screensThe header layout on large screens

At this point:

  • The header will be a fixed positioned element with a static height.
  • The navigation will be a flex container. Its contents will be vertically centered across the cross axis and horizontally distributed across the main axis.
  • The main menu will also be a flex container with vertically centered items.
  • By default, the last item (call-to-action button) of the secondary menu will be off-screen. To push it out of the screen, we’ll give its parent list transform: translateX(200px). The number 200 is derived by adding the button’s width (150px) and the amount of spacing (50px) between the list items of the secondary menu. 
  • The hamburger toggle button will be hidden. This will also contain two icons taken from CSS.gg.

The related styles:

1
/*CUSTOM VARIABLES HERE*/
2
3
.page-header {
4
  position: fixed;
5
  top: 0;
6
  left: 0;
7
  right: 0;
8
  bottom: 0;
9
  z-index: 1;
10
  height: 100px;
11
}
12
13
.page-header nav {
14
  position: relative;
15
  display: flex;
16
  justify-content: space-between;
17
  align-items: center;
18
  height: 100%;
19
  padding: 0 15px;
20
  background: var(--white);
21
  box-shadow: 0 1px 6px 0 rgba(0, 0, 0, 0.15);
22
}
23
24
.page-header ul {
25
  display: flex;
26
  align-items: center;
27
}
28
29
.page-header .main-menu {
30
  margin-left: 100px;
31
}
32
33
.page-header .main-menu li:nth-last-child(-n + 3) {
34
  display: none;
35
}
36
37
.page-header .secondary-menu {
38
  transform: translateX(200px);
39
  transition: transform 0.3s ease-out;
40
}
41
42
.page-header li:not(:last-child) {
43
  margin-right: 50px;
44
}
45
46
.page-header .btn {
47
  display: inline-block;
48
  width: 150px;
49
  text-align: center;
50
  font-weight: 900;
51
  padding: 12px 6px;
52
  border-radius: 5px;
53
  color: var(--white);
54
  background: var(--deeppurple);
55
}
56
57
.page-header a {
58
  font-size: 18px;
59
  color: var(--deeppurple);
60
}
61
62
.page-header .toggle-mobile-menu {
63
  display: none;
64
  position: absolute;
65
  top: 50%;
66
  left: 50%;
67
  transform: translate(-50%, -50%);
68
}
69
70
.page-header .toggle-mobile-menu .close-menu {
71
  display: none;
72
}
73
74
.page-header .toggle-mobile-menu path {
75
  fill: var(--deeppurple);
76
}

Medium Screens

On medium screens (≥768px and ≤1100px), its layout should look like this:

The header layout on medium screensThe header layout on medium screensThe header layout on medium screens

At this point:

  • The main menu will be absolutely positioned and shift below the header. It’ll also be hidden by default and appear when we click on the hamburger button. Additionally, its items will be equally distributed across their parent.
  • The call-to-action button is still off-screen, yet this time we’ll give its parent transform: translateX(170px) because the gap between the list items is decreased to 20 pixels. 
  • The hamburger toggle button will become visible.

The associated styles:

1
/*CUSTOM VARIABLES HERE*/
2
3
@media screen and (max-width: 1100px) {
4
  .page-header img {
5
    max-width: 140px;
6
  }
7
8
  .page-header .main-menu {
9
    display: none;
10
    position: absolute;
11
    top: 100px;
12
    left: 0;
13
    right: 0;
14
    padding: 15px;
15
    margin-left: 0;
16
    text-align: center;
17
    z-index: 1;
18
    background: var(--white);
19
    box-shadow: 0 6px 10px rgba(0, 0, 0, 0.15);
20
  }
21
22
  .page-header .main-menu li {
23
    flex: 1;
24
  }
25
26
  .page-header .secondary-menu {
27
    transform: translateX(170px);
28
  }
29
  .page-header li:not(:last-child) {
30
    margin-right: 20px;
31
  }
32
33
  .page-header .toggle-mobile-menu {
34
    display: block;
35
  }
36
}

Small Screens

Finally, on narrow screens (<768px), its layout should look like this:

The header layout on small screensThe header layout on small screensThe header layout on small screens

At this point:

  • The main menu items will be stacked. Also, the last three items will become visible.
  • The secondary menu will be hidden. 
  • There won’t be any animation on scroll. The call-to-action will be visible by default, absolutely positioned, and part of the mobile menu.

The corresponding styles:

1
@media screen and (max-width: 767px) {
2
  .page-header {
3
    height: 70px;
4
  }
5
6
  .page-header .main-menu {
7
    top: 70px;
8
    flex-direction: column;
9
    align-items: start;
10
  }
11
12
  .page-header .main-menu li {
13
    flex-grow: 0;
14
  }
15
16
  .page-header .main-menu li + li {
17
    margin-top: 15px;
18
  }
19
  
20
  .page-header .main-menu li:nth-last-child(-n + 3) {
21
    display: block;
22
  }
23
24
  .page-header .secondary-menu {
25
    display: none;
26
  }
27
28
  .page-header .toggle-mobile-menu {
29
    position: static;
30
    transform: none;
31
  }
32
33
  .page-header .btn-wrapper {
34
    position: absolute;
35
    top: 0;
36
    right: 15px;
37
  }
38
39
  .page-header .btn {
40
    width: 120px;
41
    padding: 8px 16px;
42
  }
43
}


4. Animate on Scroll

As we scroll within the page, we’ll keep track of how much we have scrolled and under a certain amount of scrolling, we‘ll smoothly animate the visibility of the header’s button by toggling the show-btn class.

The animated call-to-action buttonThe animated call-to-action buttonThe animated call-to-action button

In our example, the button will slide-in as soon as the hero section disappears, and vice versa. 

In your projects, you can easily change after how much scrolling the button should appear via the targetScroll variable. Here you can pass either a hardcoded value or a dynamic one.

Here’s the required JavaScript code:

1
const pageHeader = document.querySelector(".page-header");
2
const animatedUl = pageHeader.querySelector(".secondary-menu");
3
const showBtn = "show-btn";
4
let targetScroll = window.innerHeight - pageHeader.offsetHeight;
5
6
window.addEventListener("scroll", () => {
7
  const scrollY = this.pageYOffset;
8
9
  if (scrollY > targetScroll) {
10
    animatedUl.classList.add(showBtn);
11
  } else {
12
    animatedUl.classList.remove(showBtn);
13
  }
14
});
15
16
window.addEventListener("resize", () => {
17
  targetScroll = window.innerHeight - pageHeader.offsetHeight;
18
});

And the target CSS class:

1
.page-header .secondary-menu.show-btn {
2
  transform: none;
3
}


5. Toggle the Mobile Menu

As the last thing, to make the header fully responsive, let’s create the functionality of the mobile menu. 

As soon as we click on the hamburger button, the visibility of the mobile menu will be toggled. At that point, we’ll heavily make use of the helper classes.

Here’s the relevant JavaScript code:

1
const pageHeader = document.querySelector(".page-header");
2
const mainMenu = pageHeader.querySelector(".main-menu");
3
const openMenu = pageHeader.querySelector(".open-menu");
4
const closeMenu = pageHeader.querySelector(".close-menu");
5
const toggleMobileMenu = pageHeader.querySelector(".toggle-mobile-menu");
6
const dNone = "d-none";
7
const dBlock = "d-block";
8
const dFlex = "d-flex";
9
10
toggleMobileMenu.addEventListener("click", function () {
11
  mainMenu.classList.toggle(dFlex);
12
  if (!openMenu.classList.contains(dNone)) {
13
    this.setAttribute("aria-label", "Close Mobile Menu");  
14
    openMenu.classList.remove(dBlock);
15
    openMenu.classList.add(dNone);
16
    closeMenu.classList.remove(dNone);
17
    closeMenu.classList.add(dBlock);
18
  } else {
19
    this.setAttribute("aria-label", "Open Mobile Menu");
20
    openMenu.classList.remove(dNone);
21
    openMenu.classList.add(dBlock);
22
    closeMenu.classList.remove(dBlock);
23
    closeMenu.classList.add(dNone);
24
  }
25
});


You’ve Built a Responsive Header Animation on Scroll!

That’s all, folks! Today we discussed how to create responsive page headers with animated content on scroll. As we saw, with just a few steps we can build this kind of functionality and make eye-catching pages.

Let’s look at our creation once again:

Have you ever built such an animated header for a project in the past? If yes, what awesome techniques have you used?

As always, thanks a lot for reading!

More Tutorials With Animated Headers on Scroll

Learn more about how to animate page headers on scroll with these tutorials:

And if you’re in need of a Flexbox refresher, take a look at these tutorials:

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.