Пропустить навигацию
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 ʕ•ᴥ•ʔ

@jh3yy146 тыс. подписчиков

Открыть в 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