The most common use case for view transitions is to animate the transition between two pages. You can see an example of this in this blog, by navigating to the Blog directory page. However, it is also possible to animate the transition between two DOM states on the same page.
In order for view transitions between two elements to work, they must share a view-transition-name property in CSS. However, if two rendered elements have the same view-transition-name at the same time, ViewTransition.ready will reject and the transition will be skipped. [Source] To get the transition to work, you must ensure only one of those elements is rendered at a time. Then you can use document.startViewTransition to transition between two DOM states.
Example
Example 1: Transition Between Elements
The first example shows how to transition between two elements. The second element is hidden by default. When the button is clicked, the first element is hidden and the second element is shown. When the button is clicked again, the second element is hidden and the first element is shown.
Example 2: Transition Between States
The second example only has one element. When the button is clicked, the element changes its background color and moves to the other side of the screen. When the button is clicked again, the element changes its background color and moves back to the original side of the screen.