View Transitions in a WordPress theme: smooth page changes without a framework
A click on a link in a WordPress site replaces the whole page at once. Single-page apps animate between states, and themes copied that with AJAX navigation and animation libraries. The View Transitions API lets the browser do the animation: it takes a snapshot of the old state, renders the new one and cross-fades between them. It works for DOM updates in a page and, in Chrome since mid-2024, for normal navigations between pages of the same site.
Problem: A portfolio theme filters projects by category with AJAX. The grid is replaced in one frame, which looks jumpy, and moving between the portfolio pages flashes white.
Solution: Wrap the DOM update of the filter in document.startViewTransition() with a fallback for browsers without it, opt in to cross-document transitions with the @view-transition CSS rule, give the elements that should move between pages a unique view-transition-name, and turn the animations off for prefers-reduced-motion.
Transitions inside the page
// js/portfolio-filter.js
const grid = document.querySelector( '.ha-portfolio-grid' );
document.querySelectorAll( '.ha-portfolio-filter a' ).forEach( ( link ) => {
link.addEventListener( 'click', async ( event ) => {
event.preventDefault();
const response = await fetch( link.href, { headers: { 'X-Requested-With': 'fetch' } } );
const doc = new DOMParser().parseFromString( await response.text(), 'text/html' );
const fresh = doc.querySelector( '.ha-portfolio-grid' );
if ( ! fresh ) {
location.href = link.href;
return;
}
const update = () => {
grid.replaceChildren( ...fresh.children );
history.pushState( {}, '', link.href );
};
if ( ! document.startViewTransition ) {
update(); // no API: plain swap
return;
}
document.startViewTransition( update );
} );
} );
The data is loaded first, and only the synchronous DOM change goes into startViewTransition(). While the callback runs, the page is frozen on the snapshot, so a slow network request inside it would freeze the page for that long. The response is the normal category page parsed with DOMParser; the theme needs no extra endpoint. A complete version would also handle the back button with a popstate listener.
The default animation is a cross-fade of the whole page. It can be changed with the pseudo-elements:
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 250ms;
}
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}
Transitions between pages
For normal navigations, no JavaScript is needed. Both pages, the old and the new, must opt in with the same rule, and both must be on the same origin:
@view-transition {
navigation: auto;
}
.site-logo {
view-transition-name: ha-logo;
}
.wp-block-post-featured-image {
view-transition-name: ha-hero;
}
With navigation: auto, every same-origin navigation by link or form gets the cross-fade. An element with the same view-transition-name on the old and the new page is animated separately: the logo stays in place instead of fading, and the featured image of a post can grow from its position in an archive card to the hero of the single post when both have the name ha-hero.
The name must be unique on each page. In an archive with twelve cards, set it only on the card that was clicked, for example with a small script that adds style="view-transition-name: ha-hero" to the clicked card’s image before the navigation starts, or with inline styles per post ID (ha-hero-123) on both pages. Two elements with the same name on one page cancel the whole transition.
Load it from the theme
add_action( 'wp_enqueue_scripts', 'ha_view_transitions_assets' );
function ha_view_transitions_assets(): void {
$theme = wp_get_theme();
wp_enqueue_style( 'ha-transitions', get_theme_file_uri( 'css/transitions.css' ), array(), $theme->get( 'Version' ) );
if ( is_post_type_archive( 'portfolio' ) || is_tax( 'portfolio_category' ) ) {
wp_enqueue_script( 'ha-portfolio-filter', get_theme_file_uri( 'js/portfolio-filter.js' ), array(), $theme->get( 'Version' ), array( 'strategy' => 'defer' ) );
}
}
The strategy argument for defer exists since WordPress 6.3.
Browser support
Transitions inside the page (startViewTransition()) work in Chrome and Edge 111 (March 2023). Transitions between pages with @view-transition came in Chrome and Edge 126 (June 2024). Firefox and Safari do not support either yet (September 2024); there the code falls back to the normal behavior: an instant swap or a normal page load. That is the reason the API can be used today: it is a progressive enhancement, never a requirement.
NOTE: Cross-document transitions wait for the new page to be rendered; a slow server still feels slow, only the change itself is smoother. If the new page takes longer than about four seconds, Chrome skips the transition. Animations with CSS in general: CSS animations and keyframes.
Update (2025): Safari 18 (September 2024) added transitions inside the page and Safari 18.2 (December 2024) transitions between pages. Firefox 144 (October 2025) supports startViewTransition().