跳过导航
CSS Tip! ✨ You can create these parallax effects and image cross-fades with scroll-driven animations…
0:00 / 0:53

CSS Tip! ✨ You can create these parallax effects and image cross-fades with scroll-driven animations…

jhey ʕ•ᴥ•ʔ

@jh3yy15万 位关注者

在 X 上查看
1,722 个赞下载原帖

242,205 次观看

CSS Tip! ✨ You can create these parallax effects and image cross-fades with scroll-driven animations 🤙 img { animation: fade; animation-timeline: view(); mix-blend-mode: plus-lighter } img:last-of-type { animation-direction: reverse; } @​keyframes fade { to { opacity: 0; }} This one's fun! 😁 The trick with the cross-fading image is to make use of one animation that runs at the same time on two images inside a container. You use the same animation, animation-timeline, and animation-range. But, you use animation-direction: reverse on one of the images so they go in the opposite direction 🫶 The use of mix-blend-mode: plus-lighter; produces a better cross-fade result 💯 A viewTimeline (view()) works because you know that both images are the same height. The range you can use is img { animation-timeline: view(); animation-range: cover 45% cover 55%; } That means when the image has covered 45% of the scrollport (In this case, the window), start the animation. And finish when it has covered 55% 🎬 How about the slight parallax? This is a trick with calc(). You know the top of the small image and the big image line up. And you can do this by absolutely placing the caption outside of the small image. The trick is to translate the small image by a distance so it lines up with the bottom of the big image. You can do that like this :root { --catch-up: calc( var(--big-height) - var(--small-height) ); } @​keyframes move { to { translate: 0 var(--catch-up); }} Then drive that animation with a scroll-driven animation using the container of both images as the driver 🤙 /* section contains both images */ section { view-timeline: --container; } .img-fader { animation: catch-up both linear; animation-timeline: --container; animation-range: 50vh calc(100vh + (var(--big-height) * 0.25)); } That's it! Scroll-driven image cross-fading and parallax effects without any JavaScript. This demo will work in all browsers as there is some JavaScript in place where the API isn't supported 🤙 To do that, it uses GSAP ScrollTrigger 🏆 As always, any questions, requests, etc. hit me up! 🤙 CodePen.IO link below 👇

10 条评论

评论来自 X (Twitter) 上的原帖

  • jhey ▲🐻🎈 的头像

    @jh3yy

    Here's that @CodePen link! 🚀 You can jam on this demo today in all browsers. There is some JavaScript in place if the scroll-driven animations API isn't supported 🤙 GSAP to the rescue! 🏆

    44 个赞

  • Martin Pitt 的头像

    @Nexii

    For a little history and insight behind cross-fading with plus-lighter for folks, here is @jaffathecake's excellent article behind it:

    2 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    Good share Martin! 🙏 Almost embedded in my mind from when I started working with View Transitions 😅 The number of times I pinged Jake and it was my own problem with a mix-blend-mode thing 🤦‍♂️ This is the spot from the View Transition docs for people interested 🤙 Jake's blog is also a gold mine of interesting deep dives, etc. 👉

  • Wes 的头像

    @Ch1cx

    Wow, how it would be in responsive mode?

    1 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    Ha – Great minds... 🤙

    3 个赞

  • Jan 的头像

    @happydesign_

    So good. Thanks for tutorial.

    1 个赞

  • niftesty 🫐 的头像

    @niftesty

    Browser compatibility?

    1 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    Specifically the API is Chrome only atm. But, the demo will work everywhere as I've filled the functionality with GSAP and wrapped the API stuff in a supports query so it can be ripped out easy 🤙

  • Fermex95 的头像

    @fermex95

    Lo malo es que no está soportado en todos los navegadores

    1 个赞

  • jhey ▲🐻🎈 的头像

    @jh3yy

    The API isn't, but the demo is 🤙 Will work in all browsers