Coding Page Transitions in Vanilla JavaScript

by

• Article

3–4 minutes

read

After much research, I was unable to find a library or solution to my page transition problem. Many of the libraries I found use pushState() and the History API, or some sort of pre-fetch, to change pages as seamlessly as possible for the user. Barba.js is a good example — but I was unable to pre-fetch meta tags, which are essential for most of the SEO work that I do. It also required a larger amount of setup and added some code-bloat that slowed down the site. While there were some fancy examples, I ultimately had to find another solution.

I did find a way to do it in plain old Vanilla JavaScript, which is an (almost) perfect solution to page transitions. I was surprised that it worked so well without hearing of it before, but it does have some limitations. 

Depending on your animation, you may see a slight flash between page loads. This is an expected behavior, but we do really want that nice and smooth animation. We will look into how to avoid that as well! 

For now, let’s get right into it.

REQ Coding Transitions Vanilla Javascript Animation

We are essentially using setTimout() to stop the page from loading, performing our animation, and then loading the next page after whatever time you have given.

REQ JavaScript Transitions Code

Any time a link is clicked, we first stop the load with e.preventDefault(). We then start a timer of 500 milliseconds. Right after that, we add our css class to the body. After the 500ms, we do two things: first, check if the class has been added. Then we have to check if the <a> tag is a parent element. Many times, <a> tags will wrap other elements, such as images. The reason for having to do this check has to do with the click event and how it bubbles, but that is slightly beyond the scope of this article. Next, we load the next page! How the animation works in this example is just a simple fade out and back in, using css animations.

There you have it. Page transitions in less than 30 lines of vanilla JS. Our example here is fairly simple, but it can get as complex as you would like.

As I mentioned before, not all animations are going to look clean and smooth, particularly if you want to move elements from one page to another using that same element. However, there is one solution that I found works pretty well. To create these types of complex animations, let’s first take a look at an example I created:

Take note of the URLs here! We are able to provide a seamless transition from one page to another using the image to create some interesting movement. You can also see that the page doesn’t blink when it changes. To do this, I decided to try using a service worker file to cache the images and page content so the browser would be able to quickly show those files as the page is loading. The setup for this is also fairly simple, even if you have never worked with service workers before.

sw.js code

In your JavaScript file, add this bit of code at the top. What this is doing is checking if the browser can use the Service Worker API and then registering our service worker file (sw.js). The sw.js file in this example looks a bit like this:

REQ Vanilla JavaScript Transition Service Worker API

A very basic overview here is that this file is taking all of the static asset files and caching them in the browser on the install event. It then uses the fetch event to check the browser cache if a file on that page exists, and if so, shows the file from the cache, which will load much quicker for the user (a nice perk for SEO as well!)

The CMO Who Gave Up Sales Pitches to Build Real Relationships

The CMO Who Gave Up Sales Pitches to Build Real Relationships

Chatting with Nathan Burke of 7AI on why relationship-building outperforms traditional B2B marketing Nathan Burke is intentionally doing less of what most B2B marketers are taught to do. As CMO of 7AI, he’s opting out of the usual B2B playbook, the awkward steak dinners with a pitch attached, the conference badge scanning arms race, and…

How UVEye’s Unicorn Drives Trade Show Excitement

How UVEye’s Unicorn Drives Trade Show Excitement

Trade shows are crowded. Competitive. Expensive. Every booth promises innovation. Every brand is trying to stand out to the sea of overwhelmed and tired attendees. For AI-driven vehicle inspection company UVEye, standing out meant not just thinking creatively. It meant creating a unicorn. UVEye calls its technology an “MRI for cars.” It provides AI-driven technology that…

How WalkMe’s Melanie Pasch Humanized the Enterprise AI Adoption Problem with “AI Shame”

How WalkMe’s Melanie Pasch Humanized the Enterprise AI Adoption Problem with “AI Shame”

Ask an executive how many software applications their company uses, and they’ll probably guess 30 or 40. The average organization, according to research by digital adoption platform (DAP) pioneer WalkMe, actually runs about 625 applications. This staggering digital ecosystem is where most tech investments stall, not because the technology is poor, but because employees can’t…

From $200M ARR to Pre-Seed: How Karina Lawrence Rewrites the Marketing Playbook for Early-Stage Startups

From $200M ARR to Pre-Seed: How Karina Lawrence Rewrites the Marketing Playbook for Early-Stage Startups

When you’ve helped scale a developer-focused company from roughly $200M to nearly $250M in ARR, you know what “grown-up” marketing looks like. Today, though, Karina Lawrence is back at the very beginning—leading marketing at Macrovo, a pre-seed, ~10-person startup that blends AI and human expertise to help financial institutions make faster, smarter decisions. It’s a…

B2B Videos You Actually Want to Watch? Meet Jared Evers of Medallia.

B2B Videos You Actually Want to Watch? Meet Jared Evers of Medallia.

For Jared Evers and his small and scrappy content team at Medallia – provider of customer and experience software – if you can’t do something stellar, there’s no sense in doing it at all. For proof, check out how the team is pushing the boundaries of corporate videos with Experience Now, Medallia’s own streaming platform.…

How HII’s Jaime Orlando Builds Connection, Culture, and Momentum Inside a Legacy Brand

How HII’s Jaime Orlando Builds Connection, Culture, and Momentum Inside a Legacy Brand

Q: Jaime, for those who might not know HII Mission Technologies, can you give us a quick overview of what your team does? Jaime Orlando Absolutely. HII as a company has an incredible legacy. It’s America’s largest shipbuilder, with more than 135 years of experience. About 75% of HII’s business comes from shipbuilding at our…

How Jenifer Kern Helped Qu Redefine Restaurant Tech

How Jenifer Kern Helped Qu Redefine Restaurant Tech

On the Radar sat down with Jenifer Kern, CMO of Qu, to talk about how she helped create a new category in restaurant technology, why maintaining industry focus has been key to business growth, and what it means to elevate marketing in a longstanding industry undergoing rapid transformation. Q: When you joined Qu, what did the industry…

From The New York Times to Muck Rack: Linda Zebian on Knowing What’s Newsworthy

From The New York Times to Muck Rack: Linda Zebian on Knowing What’s Newsworthy

Linda Zebian knows how to tell a good story. As VP of Communications at Muck Rack, she leads a lean, high-impact team responsible for brand, content, product marketing, internal comms, and more. Her approach is grounded in the instincts she developed over 10 years in corporate comms at The New York Times, where she learned…

How Sam Baldridge is Turning Culture Into a Competitive Edge

How Sam Baldridge is Turning Culture Into a Competitive Edge

At Applied Systems, Sam Baldridge wears a lot of hats. Officially, she’s the Senior Communications and Culture Specialist. Unofficially, she might be better known as the “Vibes Director.” Sam is part of a small but mighty three-person team tasked with building internal connection, shaping employer branding, and turning culture into a competitive advantage.  We caught…

How Kristina McConnell Uses Precision and AI to Power Account-Based Marketing at H1

How Kristina McConnell Uses Precision and AI to Power Account-Based Marketing at H1

A Director of Marketing at H1, Kristina McConnell brings structure, creativity, and a test-and-learn mindset to every campaign she touches. With a small team and a niche audience in the pharma space, she has helped transform H1’s account-based marketing (ABM) approach into a tightly aligned, data-driven engine. Her team goes far beyond basic alignment with sales.…

CONTACT US
CONTACT US

WE HELP BRANDS OWN WHAT’S NEXT

Our integrated PR and digital campaigns build reputations, drive growth, and shape conversations that define markets. Let’s talk about how we can help you do the same.