DEV Community

iDev-Games
iDev-Games

Posted on

1

πŸ“± Mobile Rubber Banding Effect with Trig.js

Ever wanted to achieve that elastic rubber banding effect when scrolling to the top or bottom of a page? 🀯 With Trig.js, it's possible with the default CSS classes trig-scroll-top and trig-scroll-bottom. Let's dive in! πŸ”₯


🎬 The Effect in Action

Here's what we'll be creating:

➑️ When scrolling to the top, elements stretch down and snap back.
➑️ When scrolling to the bottom, elements stretch up before snapping.

Check out the demo: CodePen Example


πŸ› οΈ Setting It Up (Super Simple!)

1️⃣ Include Trig.js

If you haven’t already, grab Trig.js:

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

Or install via npm:

npm install trig-js
Enter fullscreen mode Exit fullscreen mode

2️⃣ Style the Rubber Banding Effect with CSS

Now, let’s add some simple CSS magic to create the stretch effect:

.trig-scroll-top .pageContainer{
  animation:rubberBandTop 1.5s ease-out;
}
.trig-scroll-bottom .pageContainer{
  animation:rubberBandBottom 1.5s ease-out;
}
@keyframes rubberBandTop {
  10% {
    transform:translateY(0px);
  }

  20% {
    transform:translateY(100px);
  }

  40% {
    transform:translateY(-20px);
  }

  60% {
    transform:translateY(40px);
  }

  100% {
    transform:translateY(0px);
  }
}
@keyframes rubberBandBottom {
  10% {
    transform:translateY(0px);
  }

  20% {
    transform:translateY(-100px);
  }

  40% {
    transform:translateY(20px);
  }

  60% {
    transform:translateY(-40px);
  }

  100% {
    transform:translateY(0px);
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ† Why Use Trig.js for This?

βœ… CSS-only animations β†’ super smooth πŸ’¨
βœ… Lightweight (4KB!) β†’ perfect for mobile πŸ“±

This technique is perfect for mobile web apps, iOS-style scroll effects, or just making your site feel extra polished. ✨


πŸ’¬ What Do You Think?

Would you use this in your next project? Let me know in the comments! πŸ”₯

And if you found this useful, drop a ⭐ on Trig.js on GitHub! πŸš€

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay