DEV Community

iDev-Games
iDev-Games

Posted on

1

Modern Floating Header With Article Progress Bar Using Trig.js

A smooth floating header that hides when scrolling down but reappears when scrolling up or reaching the bottom of the page. Plus, an article progress bar using global CSS variables for seamless performance.

๐Ÿš€ Live Demo on CodePen

Why Use Trig.js?

Trig.js makes scroll-based animations lightweight and efficient by leveraging CSS variables and requestAnimationFrame. Unlike bloated alternatives, it keeps animations smooth and performant.

Features in This Example

โœ… A floating header that hides on scroll down and reappears on scroll up or at the page bottom.

โœ… A dynamic progress bar indicating how much of the article has been read.

โœ… Uses data-trig-global="true" to make CSS variables add to the body tag.

โœ… Pure CSS-based transformations for optimal performance.

How It Works

This example uses trig-scroll-up, trig-scroll-down, and trig-scroll-bottom classes to control the header visibility. The progress bar utilizes --trig-article as a CSS variable to track scroll progress.

HTML Structure

<header>
    <div class="header slide">
        <div class="logo">Trig.js</div>
        <div class="menu">
            <ul>
                <li class="mobileMenu">&#8801;</li>
                <li>Home</li>
                <li>About</li>
                <li>Blog</li>
                <li>Contact Us</li>
            </ul>
        </div>
        <div class="progress">
            <div class="progressBar"></div>
        </div>
    </div>
</header>
Enter fullscreen mode Exit fullscreen mode

CSS Styling

.trig-scroll-down .slide {
    transform: translateY(calc(-100% + 10px));
}
.trig-scroll-top .slide,
.trig-scroll-bottom .slide,
.trig-scroll-up .slide {
    transform: translateY(0px);
}
.progressBar:after {
    content: "";
    height: 10px;
    width: 100%;
    transform: scaleX(var(--trig-article));
    transform-origin: left;
    background-color: #1b8237;
    display: block;
    transition: transform ease-out 0.3s;
}
Enter fullscreen mode Exit fullscreen mode

JavaScript (Trig.js)

<script src="https://cdn.jsdelivr.net/npm/trig-js/src/trig.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

No extra JS neededโ€”Trig.js automatically updates the CSS variables based on scrolling.

More Trig.js Examples

๐Ÿ”น Best AOS (Animation on Scroll) Libraries in 2025

๐Ÿ”น Mobile Rubber Banding Effect with Trig.js

Get Started With Trig.js

Let me know what you think in the comments! ๐Ÿš€

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series ๐Ÿ“บ

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series ๐Ÿ‘€

Watch the Youtube series

๐Ÿ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someoneโ€™s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay