import { preloadImages } from '../utils.js'; // Variable to store the Lenis smooth scrolling object let lenis; // Selecting DOM elements const contentElements = [...document.querySelectorAll('.content--sticky')]; const totalContentElements = contentElements.length; // Initializes Lenis for smooth scrolling with specific properties const initSmoothScrolling = () => { // Instantiate the Lenis object with specified properties lenis = new Lenis({ lerp: 0.2, // Lower values create a smoother scroll effect smoothWheel: true // Enables smooth scrolling for mouse wheel events }); // Update ScrollTrigger each time the user scrolls lenis.on('scroll', () => ScrollTrigger.update()); // Define a function to run at each animation frame const scrollFn = (time) => { lenis.raf(time); // Run Lenis' requestAnimationFrame method requestAnimationFrame(scrollFn); // Recursively call scrollFn on each frame }; // Start the animation frame loop requestAnimationFrame(scrollFn); }; // Function to handle scroll-triggered animations const scroll = () => { contentElements.forEach((el, position) => { const isLast = position === totalContentElements-1; gsap.timeline({ scrollTrigger: { trigger: el, start: 'top top', end: '+=100%', scrub: true } }) .set(el, { transformOrigin: `50% ${ isLast ? 100 : 0 }%` }) .to(el, { ease: 'none', scaleY: 0 }, 0) // Animate all the content inner elements .to(el.querySelectorAll('.content__title, .content__text, .content__img'), { ease: 'back.in(0.1)', scale: 1.7, opacity: 0 }, 0); }); }; // Initialization function const init = () => { initSmoothScrolling(); // Initialize Lenis for smooth scrolling scroll(); // Apply scroll-triggered animations }; preloadImages('.content__img').then(() => { // Once images are preloaded, remove the 'loading' indicator/class from the body document.body.classList.remove('loading'); init(); });