Technical Guide Craffcodes

Smooth Page Transitions with Swup and GSAP

Learn how to build smooth SPA-like transitions using Swup and GSAP.

arrow_back Back to Blog
Stage 01

Introduction to Page Transitions

Modern websites aim to deliver app-like experiences with smooth transitions between pages. Traditional page reloads break continuity and feel outdated. Libraries like Swup allow you to create seamless navigation experiences without full reloads.

When combined with GSAP, Swup enables highly controlled, performant animations that enhance user experience without compromising speed.

Stage 02

How Swup Works

Swup intercepts link clicks, fetches the new page via AJAX, and replaces the content inside a specified container. This allows you to animate transitions between old and new content.


      const swup = new Swup({
  containers: ['#swup'],
  animateHistoryBrowsing: true
});
    
Stage 03

Setting Up the Transition Container

Wrap your page content inside a container that Swup will replace during navigation.


      <div id="swup">
  <main>
    <!-- page content -->
  </main>
</div>
    
Stage 04

Integrating GSAP Animations

GSAP allows you to animate elements during page transitions. You can hook into Swup lifecycle events to trigger animations.


      swup.hooks.on('visit:start', () => {
  gsap.to('#swup', {
    opacity: 0,
    y: 40,
    duration: 0.4
  });
});
    

      swup.hooks.on('content:replace', () => {
  gsap.from('#swup', {
    opacity: 0,
    y: 40,
    duration: 0.5
  });
});
    
Stage 05

Creating Overlay Transitions

A popular technique is using a full-screen overlay that animates in and out during navigation. This creates a smooth, cinematic transition experience.


      gsap.to('.transition-layer', {
  x: '0%',
  duration: 0.9,
  ease: 'power3.inOut'
});
    

Ensure only internal links are handled by Swup to avoid conflicts with external navigation.


      linkSelector: "a[href^="/"]:not([target="_blank"])
    
Stage 07

Reinitializing Scripts After Navigation

Since Swup replaces page content dynamically, you must reinitialize animations and scripts after each transition.


      swup.hooks.on('content:replace', () => {
  window.craffcodesReinitAnimations?.();
});
    
Stage 08

Performance Considerations

Keep animations lightweight and avoid heavy DOM manipulations. Use transform and opacity properties to ensure smooth performance.

Stage 09

Common Pitfalls

Avoid double initialization of Swup, ensure correct container selection, and prevent duplicate event listeners which can cause glitches.

Stage 10

Conclusion

Swup combined with GSAP enables smooth, app-like navigation experiences on the web. By properly managing lifecycle hooks, animations, and performance, you can create seamless transitions that elevate user experience.

Summary

Performance

Apply these steps incrementally to preserve UX while improving runtime and load stability.

Maintainability

Keep implementation modular so future updates can be tested and shipped with confidence.

Related Blogs