A page transition hides the jump between pages. The idea is the same everywhere: animate out, change page, animate in. The code below was tested with GSAP 3.15. The multi-page version runs as written, and the Next.js version was type-checked against Next.js and @gsap/react.
On a multi-page site
This works on any site where each link loads a new HTML page, from a hand-built site to a CMS. A full-screen cover sits over the page. It lifts when the page loads, and drops back down when you click a link, before the browser moves on.
Add the cover as the first thing in <body>, and a one-line script in <head> so the page starts covered only when JavaScript is running.
<head>
<script>document.documentElement.classList.add("js");</script>
</head>
<body>
<div class="transition" aria-hidden="true"></div>
…
</body>.transition {
position: fixed;
inset: 0;
z-index: 100;
background: #111;
transform: scaleY(0);
pointer-events: none;
}
.js .transition { transform: scaleY(1); }Then load GSAP and this script on every page.
const cover = document.querySelector(".transition");
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
// Uncover the page
function reveal() {
gsap.fromTo(
cover,
{ scaleY: 1 },
{ scaleY: 0, transformOrigin: "top", duration: reduce ? 0 : 0.6, ease: "power3.inOut" },
);
}
reveal();
// Back and forward can restore a page from cache with the cover still up
window.addEventListener("pageshow", (e) => {
if (e.persisted) reveal();
});
// Cover the page, then go
document.addEventListener("click", (e) => {
const link = e.target.closest("a");
if (!link || link.target || link.hasAttribute("download")) return;
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
const url = new URL(link.href);
if (url.origin !== location.origin || url.pathname === location.pathname) return;
e.preventDefault();
gsap.fromTo(
cover,
{ scaleY: 0 },
{
scaleY: 1,
transformOrigin: "bottom",
duration: reduce ? 0 : 0.5,
ease: "power3.inOut",
onComplete: () => location.assign(url.href),
},
);
});The checks at the top of the click handler leave some links alone: links to other sites, links that open a new tab, downloads, links to the same page, and clicks with a modifier key held down.
The pageshow listener matters more than it looks. When someone presses Back, the browser may restore the previous page exactly as they left it, cover and all. Without it, they'd come back to a blank screen.
In the Next.js App Router
The App Router swaps pages without a full reload, so you animate the page content instead of a cover. Two small pieces do it.
First, app/template.tsx. Unlike a layout, a template mounts fresh on every navigation, so its animation runs each time you arrive on a page.
"use client";
import { useRef } from "react";
import { gsap } from "gsap";
import { useGSAP } from "@gsap/react";
gsap.registerPlugin(useGSAP);
export default function Template({ children }: { children: React.ReactNode }) {
const ref = useRef<HTMLDivElement>(null);
useGSAP(() => {
gsap.from(ref.current, { autoAlpha: 0, y: 24, duration: 0.6, ease: "power3.out" });
});
return (
<div ref={ref} data-page>
{children}
</div>
);
}Second, a link that plays the exit before it navigates. Use it in place of Link wherever you want the transition.
"use client";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { gsap } from "gsap";
type Props = Omit<React.ComponentProps<typeof Link>, "href"> & { href: string };
export function TransitionLink({ href, onClick, ...rest }: Props) {
const router = useRouter();
const pathname = usePathname();
return (
<Link
href={href}
{...rest}
onClick={(e) => {
onClick?.(e);
if (e.defaultPrevented || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
if (href === pathname) return; // same page: template.tsx would not re-run
e.preventDefault();
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
gsap.to("[data-page]", {
autoAlpha: 0,
y: -24,
duration: reduce ? 0 : 0.4,
ease: "power2.in",
onComplete: () => router.push(href),
});
}}
/>
);
}The exit fades the current [data-page] out. Then router.push loads the next page, template.tsx mounts a new [data-page], and it fades in. Install @gsap/react alongside gsap for the useGSAP hook.
Or let the browser do it
For a simple cross-fade between pages on a multi-page site, CSS alone can do it with the View Transitions API. Add this to every page's CSS.
@view-transition {
navigation: auto;
}Browsers that support it fade between pages, and the rest just load the page as normal. Reach for GSAP when you want a designed transition: a wipe, a cover with your logo, or text that lands as the page arrives.
Get the details right
- Keep it short. Around half a second each way. People click links to get somewhere, and a long transition on every click wears thin.
- Respect reduced motion. Both versions above set the duration to zero when someone has asked their system for less motion, so pages still change, just instantly.
- Keep the nav usable. If the cover sits over your navigation, people can't click again while it plays. Put the nav above the cover, or keep the transition short.





