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 link below 👇
Had this tab open since building this demo 😅
Might be time to build the rest of the demo for you 🤙
Combos of position: sticky and some JavaScript fun to dial in the scroll animation parts ✨
No filling up the words with background-position like the other demo either 😁