Blog Layout

Creating a Dynamic Header Animation on a Duda Website

Aj Pfeil • December 6, 2024

In This Article:

Creating a Dynamic Header Animation on a Duda Website

A laptop is open to a page that says the blog topic

Incorporating dynamic elements into your website can enhance both user experience and design aesthetics. A straightforward yet powerful feature is a scrolling header that disappears as users scroll down and reemerges when they scroll up. This is especially beneficial for maintaining accessible navigation while conserving valuable screen space. Here's an overview of how it functions and how you can integrate it into your Duda website.


What Does This Code Do?

This script enhances your website header, making it both intelligent and dynamic: 

When you scroll down, the header gracefully slides out of sight, providing more room for your content. 

When you scroll up, the header reemerges, ensuring easy access to navigation links. 

This effect is accomplished by sensing the direction of the user’s scroll and applying CSS classes to the header for smooth animation.


How It Works

The JavaScript code monitors scroll events and evaluates the user's scroll position. Based on whether the user is scrolling upward or downward, it adds a scroll-up or scroll-down class to the header element. These classes initiate CSS animations that move the header in or out of view. The corresponding CSS specifies the visual behavior of the header: In its Default State, the header remains fixed at the top of the page. When scrolling down, the scroll-down class applies a transformation that conceals the header. Conversely, when scrolling up, the scroll-up class reverses the transformation, revealing the header once more.


The Code - JavaScript

<script>

    dmAPI.runOnReady('runOnReady',function(){

        let header = $('.dmHeaderContainer');

        let lastScroll = 100;

       

        $(window).scroll(function() {

          let currentScroll = $(window).scrollTop();

         

          if (currentScroll <= 100) {

            header.removeClass('scroll-up');

            return;

          }

         

          if (currentScroll > lastScroll) {

            header.removeClass('scroll-up').addClass('scroll-down');

          } else if (currentScroll < lastScroll) {

            header.removeClass('scroll-down').addClass('scroll-up');

          }

          lastScroll = currentScroll;

        });

    });

</script>


The Code - CSS

.dmHeaderContainer.scroll-down {

  transform: translate3d(0, -100%, 0);

}


.dmHeaderContainer.scroll-up {

  transform: none;

}


.dmHeaderContainer {

    position: fixed;

    top: 0;

    width: 100%;

    left: 0;

    z-index: 1000;

    transition: transform 0.4s;

}


How to Implement It

Add the JavaScript:

Navigate to Site Settings > Body-End html in your Duda editor.

Paste the JavaScript code.


Add the CSS:

Go to Dev Mode - Navigate to Site.CSS Folder.

Add the CSS code.


Test the Feature:

Preview your website and scroll up and down to see the header’s dynamic behavior.


Why Use This Feature?

Enhances Usability: Ensures navigation is readily accessible when required. Refines Design: Delivers a sleek, contemporary user experience. Tailorable: Modify the animation timing or look to align with your brand identity. This compact yet impactful feature can significantly boost both the functionality and visuals of your Duda website. Try it out and witness the improvement it brings!

A landing page for a website with a hover effect made easy.
By Aj Pfeil December 10, 2024
The Hover Float Effect is a versatile tool to enhance your web design. It’s subtle yet impactful, adding a layer of interactivity that keeps users engaged. With just a few lines of CSS, you can elevate the look and feel of your site, making it more dynamic and modern.
A close up of a laptop with a website on the screen
By Aj Pfeil December 7, 2024
A flashing tab title is a fun and effective way to grab your visitors’ attention when they navigate away from your site. By alternating between two messages in the browser tab, you can encourage users to return, highlight special offers, or just add a creative flair to your website.
By Aj Pfeil December 4, 2024
Adding a Table of Content to your Duda Websites A Table of Contents (TOC) is a fantastic addition to any webpage, especially for blogs or long-form content. It enhances the user experience by providing quick navigation links to different sections on a page, improving readability and engagement. In this guide, I’ll show you how to implement a fully functional Table of Contents on your Duda website using a simple JavaScript snippet.
A light bulb hanging from the ceiling with the words custom website scrollbars
By Aj November 3, 2024
Learn how to enhance your Duda Websites customization with custom looking scrollbars across the site, or on single pages.
More Posts
Share by: