Introduction
The View animation example shows how to animate layout changes using the browser's View Transitions API with Motion's animateView function. When you click the toggle, the handle smoothly slides from one side to the other - but instead of manually animating the position, we just change the CSS layout and let animateView handle the animation automatically.
This example also uses the press gesture from Motion to detect clicks, and the spring easing function to create a bouncy animation.
One of the biggest benefits of using animateView over traditional CSS transitions is that it can animate between completely different layouts. With CSS, you'd need to manually animate the translate property. With animateView, you simply change the layout (like switching from justify-content: flex-start to flex-end) and Motion figures out the animation for you.
Get started
Let's start by creating a simple toggle button with a handle inside:
<button class="toggle-container">
<div class="toggle-handle"></div>
</button>
<script type="module">
let isOn = false
</script>
<style>
/** Copy styles from example source code */
</style>
The button uses flexbox with justify-content to position the handle. When we add the on class later, the handle will jump to the other side.
Notice the .toggle-handle has a special property in the CSS: view-transition-name: toggle-handle. This tells the browser which element should be animated when we use the View Transitions API. You can think of it like giving the element an ID for animations.








