Skip navigation
CSS Tip! 📜 You can use scroll-driven animations to progressively enhance collapsing a floating call…
0:00 / 0:47

CSS Tip! 📜 You can use scroll-driven animations to progressively enhance collapsing a floating call…

jhey ʕ•ᴥ•ʔ

@jh3yy148K followers

Visit on X
1,616 likesDownloadOriginal post

178,031 views

CSS Tip! 📜 You can use scroll-driven animations to progressively enhance collapsing a floating call to action 🤏 .cta { animation: shrink; animation-timeline: scroll(); animation-range: 0 100px; } @​keyframes shrink { to { width: 48px; } } That's the gist of it. Use the body scroll position with animation-timeline: scroll(). Define the animation-range as when you have scrolled 100px. There's a little more though 🤓 That would "scrub" the width animation. Ideally, you want to trigger that animation. You could animate a custom property with steps() timing and use that to define the width ✨ @​property --scrub { syntax: ' '; inherits: true; initial-value: 0; } body { animation: scrub both steps(1, end); animation-timeline: scroll(); animation-range: 0 100px; } Then transition the --scrub property on the CTA and use it for the width 🤙 .cta { transition: --scrub 0.2s; width: calc(48px + (120px * (1 - (var(--scrub) / 100)))); } Other animations are a matter of preference and timing. For example, you could then make the hand wave, scale down the size, and then slide a gradient across 😉 They have the same structure and technique as the original concept. Waving the hand? 👋 Run it twice, offset the transform-origin. .hand { animation: wave both linear 2; animation-timeline: scroll(); animation-range: 30vh 50vh; transform-origin: 65% 75%; } @​keyframes wave { 50% { rotate: 20deg; } } How's it progressively enhanced? Wrap everything in a @​supports query and a @​media query. If there isn't support, users still get a good experience. It's a floating action button that's circular and already collapsed 🤙 @​supports(animation-timeline: scroll()) { @​media(prefers-reduced-motion: no-preference) {...} } Definitely have a play with the code. Amazing what we're going to be able to do with CSS alone! 🔥 CodePen.IO link below! 👇

10 Comments

Comments from the original post on X (Twitter)

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Here's that @CodePen link! 🚀 Scroll-driven animations are supported in Chromium browsers ⚡️ Your users should get a decent experience in all! 😎 No JavaScript? it'll run if supported and won't if not 🤙 JavaScript and it's not supported? GSAP 😉

    28 likes

  • Igor bedesqui's profile picture

    @bedesqui

    the sheer amount of cool stuff you can do with scroll animations 🤯

    6 likes

  • Hannah Leopold's profile picture

    @hannahsox12

    This looks cool, but if I were the user it would annoy me so much. As a dev, I'd want to add in something so once the user clicks on it once, it goes away.

    3 likes

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Totally agree! 💯 Implemented this one to see how it would work. Inspo is taken from Android apps such as the Gmail client. In that app, the "Compose" button does this behavior. Shrinks and grows on scroll.

    4 likes

  • Zachary Stoltz's profile picture

    @zacharystoltz

    Can this be used for things “scrolled into view” natively? Or would you still need JS with the web api…. Which for the life of me can’t remember?

    3 likes

  • Emilio Sánchez's profile picture

    @emlez_

    Would love Intercom or more chatbots did this!

    2 likes

  • 𝕺𝖝𝕾𝖎𝖒𝖔𝖓's profile picture

    @dabi_simon

    @setemiojo Love this CSS tip! Scroll-driven animations can really elevate a web design!

    2 likes

  • Helge Ahrens's profile picture

    @Tylopilus

    For scroll driven animations can we also use it for relative values? I always see absolut numbers but sometimes I don’t know when the comp is in viewport

    2 likes

  • jhey ▲🐻🎈's profile picture

    @jh3yy

    Yes totally! 💯 The API for that is view() Check this demo out 👇 The heading is animated based on its position within the viewport using the range keywords entry and cover. The animation-timeline is set to view()

    3 likes

  • Javaid Shah's profile picture

    @JvShah124

    That's cool 🤩

    1 like