DEV Community

iDev-Games
iDev-Games

Posted on

πŸ“± 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! πŸš€

Top comments (0)