I

CREATE

WEBSITES!

GSAP Smooth Scrolling Effect

How to Implement Smooth Scrolling with GSAP and Custom Code in Elementor Pro

Smooth scrolling is a great way to enhance the user experience on your website, providing a seamless feel when navigating through content. In this tutorial, we’ll show you how to implement smooth scrolling and combine it with GSAP animations using Elementor Pro’s custom code functionality.

Install Elementor Pro

Before you can use custom code within Elementor, make sure you have the Pro version installed. Elementor Pro unlocks the ability to add custom HTML, CSS, and JavaScript to your website.

Access Elementor Pro’s Custom Code Feature

  1. From your WordPress dashboard, go to Elementor > Custom Code.
  2. Click the Add New button to create a new custom code snippet.
  3. Name your code, something like “Smooth Scrolling with GSAP”, and set the location to load it in the head of your website.

Adding the CSS for Smooth Scrolling

Now, let’s add the necessary CSS for smooth scrolling. This CSS will adjust the page behavior and control scrolling interactions.

				
					<style>
html.lenis {
  height: auto;
}

.lenis.lenis-smooth {
  scroll-behavior: auto !important;
}

.lenis.lenis-smooth [data-lenis-prevent] {
  overscroll-behavior: contain;
}

.lenis.lenis-stopped {
  overflow: hidden;
}

.lenis.lenis-scrolling iframe {
  pointer-events: none;
}
</style>

				
			

Adding the JavaScript for GSAP Animations and Smooth Scrolling

GSAP (GreenSock Animation Platform) is a powerful tool for creating animations. Here, we will use it in combination with the Lenis smooth scrolling library to bring a fluid, animated scrolling experience to your site.

				
					<script src="https://unpkg.com/@studio-freight/lenis@1.0.34/dist/lenis.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/ScrollTrigger.min.js"></script>

<script>
document.addEventListener("DOMContentLoaded", function() {
  // Initialize Lenis
  const lenis = new Lenis({
    duration: 5.2, // Customize duration if needed
    easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), // Easing function
    smoothWheel: true, // Allow smooth scrolling with the mouse wheel
    smoothTouch: false // Disable smooth scrolling for touch devices if desired
  })

  // Trigger Scroll Updates
  lenis.on('scroll', (e) => {
    console.log(e) // This logs the scroll event
    ScrollTrigger.update() // Update ScrollTrigger animations
  })

  // GSAP integration with Lenis for smooth animations
  gsap.ticker.add((time) => {
    lenis.raf(time * 1000) // Custom requestAnimationFrame for smoother integration
  })

  // Disable lag smoothing to match Lenis behavior
  gsap.ticker.lagSmoothing(0)

  // Start the Lenis scroll loop
  function raf(time) {
    lenis.raf(time)
    requestAnimationFrame(raf)
  }
  requestAnimationFrame(raf)
})
</script>

				
			

Implementing the Code on Your Website

  1. Copy the entire script and paste it into the Custom Code section in Elementor.
  2. Set the display condition to apply the script across the entire site or on specific pages where you want to enable smooth scrolling.
  3. Publish your changes.

Customizing Your Animations

You can modify the gsap.to() function to customize how different elements behave when the user scrolls. Use different properties like x, y, opacity, or scale to achieve unique effects.

Conclusion

With just a few lines of code, you’ve added smooth scrolling and GSAP animations to your Elementor-powered website! This technique will improve the look and feel of your site, keeping users engaged as they browse.

Be sure to test your page for performance and compatibility on both desktop and mobile devices to ensure the best experience for all users.

This function has been disabled for Jeh Creates.

Start Project

MENU