DEV Community

iDev-Games
iDev-Games

Posted on

1

πŸš— CSS Car Scroll Animation Using Trig.js

Ever wanted to animate a car driving along a road using only CSS and Trig.js? With Trig.js v4.2.0, you can control animations based on scroll progressβ€”no JavaScript required!

πŸ‘‡ Check out the live demo:


✨ How It Works

This animation uses Trig.js to update the data-trig-var attribute in increments of 10, allowing the car to move smoothly at different scroll positions.

πŸ”Ή Key Features

βœ… The road moves dynamically as you scroll πŸ“œ

βœ… The car changes position using data-trig-var 🚘

βœ… The animation is entirely CSS-based 🎨


πŸ”§ The Code

πŸ“Œ Setting Up Trig.js

<div class="pinContainer" data-trig data-trig-var="true">
  <div class="pinned">
    <div class="road">
      <div class="carBox">
        <div class="carContainer">
          <div class="car"></div>
          <div class="wheel front"></div>
          <div class="wheel back"></div>
        </div>
      </div>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

🚘 Car Movement

Using data-trig-var, we adjust the car’s position dynamically based on the scroll progress:

[data-trig-var="10"] .carBox { transform: translateY(10px) translateX(-10%); }
[data-trig-var="50"] .carBox { transform: translateY(0px) translateX(50%); }
[data-trig-var="100"] .carBox { transform: translateY(100px) translateX(110%); }
Enter fullscreen mode Exit fullscreen mode

This means the car smoothly moves along the road as you scroll! πŸš—πŸ’¨


Help Trig.js Grow! 🌱

If you love what Trig.js brings to the table, here’s how you can help:
βœ… ⭐ Star the repo on GitHub

βœ… Use it in your projects and give feedback! πŸ“

Want to try it out? Check out the full CodePen demo! 🎨πŸ”₯

What do you think of this effect? Let me know in the comments! πŸ’¬πŸš€

DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM appsβ€”no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

πŸ‘‹ Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's dayβ€”leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay