DEV Community

iDev-Games
iDev-Games

Posted on

1

Sun Header With Rising Logo Bouncing Letters Using Trig.js ☀️

Ever wanted to create a stunning animated header where each letter bounces into place as you scroll? With Trig.js, you can achieve this effect effortlessly! 🎉

Check out the live example here:

How It Works 🛠️

This example features a sunrise-inspired header where:

  • The background sun scales and rotates dynamically.
  • Each letter in the Trig.js logo rises as you scroll.

We use Trig.js to smoothly control the scroll animations. The key part of the effect is how we selectively apply the .selected class to each letter based on the scroll position.

The JavaScript Magic ✨

function checkTrig() {
  const element = document.querySelector("header");
  const trigValue = parseFloat(
    getComputedStyle(element).getPropertyValue("--trig")
  );

  const letters = document.querySelectorAll(".letter");
  const index = Math.min(
    Math.max(Math.floor((trigValue - 50) / 10), 0),
    letters.length
  );

  letters.forEach((letter, i) => {
    if (i === index) {
      letter.classList.add("selected");
    } else {
      letter.classList.remove("selected");
    }
  });

  requestAnimationFrame(checkTrig);
}

checkTrig();
Enter fullscreen mode Exit fullscreen mode

Breaking It Down 🔍

  • trigValue is retrieved from the --trig CSS variable, which updates dynamically based on scroll.
  • We calculate an index to determine which letter should rise based on trigValue.
  • Only one letter at a time gets the .selected class, creating a smooth wave-like effect.

The CSS Magic 🎨

.letter {
  display: inline-block;
  transition: transform ease-out 0.3s;
}

.letter.selected {
  transform: translateY(-30px);
}
Enter fullscreen mode Exit fullscreen mode

This ensures that when a letter gets the .selected class, it rises smoothly! 🕊️

Try It Out 🚀

Want to explore more? Check out the full Trig.js repository here:

🔗 Trig.js GitHub Repo

Let me know what you think or if you have any ideas for improvements! Happy coding! 🎨✨

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 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