Overview
The translate-along-path built-in effect is used to animate an element by moving it along a predefined SVG path. This effect is commonly utilized in animations where you want an object to follow a curved or complex route rather than a straight line. The motion is determined by the geometry of the SVG path, allowing for precise and visually appealing transitions.
You can animate any DOM element using the translate-along-path(<target: {svg-path}>) alias. In this context, target: {svg-path} specifies the SVG path element along which the DOM element will move.
Note: In order to maintain the right behavior, always include the viewBox on the parent `svg element.
Syntax
Default
The default translate-along-path parameters:
| Name |
Default value |
Accepted values |
Required option |
translate-along-path |
- |
- |
path |
{trigger}:translate-along-path({options})
Examples
These example show the translate-along-path built-in effect in action using the animate, hover, scroll and mouse triggers, but you can use any of the supported triggers.
Note: To simulate the movement of an element along its target path, wrap both the element and the svg (containing the path) inside a parent wrapper with position: relative. Then, apply position: absolute; top: 0; left: 0; to your element.
Animate
We have defined the animation options by the <duration: 4s, infinite, target: #example-path-02> that sets the duration to 4s and make sure that animation plays forever.
<div class="relative">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="198.2" height="188.8" viewBox="0 0 198.2 188.8" style="enable-background:new 0 0 198.2 188.8" xml:space="preserve">
<path id="example-path-02" style="fill:none;stroke:#d7d7d7;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10" d="m99.1 3 29.7 60.2 66.4 9.6-48.1 46.8 11.4 66.2-59.4-31.3-59.4 31.3L51 119.6 3 72.8l66.4-9.6z" />
</svg>
</div>
Hover
When you want to reveal a path on hover, it’s better to define the target element (usually the parent element) and apply the hover effect to it using the target parameter. We use hover(parent), where the parent refers to the nearest wrapper element.
<div class="relative">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="198.2" height="188.8" viewBox="0 0 198.2 188.8" style="enable-background:new 0 0 198.2 188.8" xml:space="preserve">
<path id="example-path-03" style="fill:none;stroke:#d7d7d7;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10" d="m99.1 3 29.7 60.2 66.4 9.6-48.1 46.8 11.4 66.2-59.4-31.3-59.4 31.3L51 119.6 3 72.8l66.4-9.6z" />
</svg>
</div>
It is possible to create a translate along path animation on scroll using the scroll trigger.
<div class="relative">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="198.2" height="188.8" viewBox="0 0 198.2 188.8" style="enable-background:new 0 0 198.2 188.8" xml:space="preserve">
<path id="example-path-03" style="fill:none;stroke:#d7d7d7;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10" d="m99.1 3 29.7 60.2 66.4 9.6-48.1 46.8 11.4 66.2-59.4-31.3-59.4 31.3L51 119.6 3 72.8l66.4-9.6z" />
</svg>
</div>
Mouse
Similar to above, you can use a mouse or pointer trigger to create a revealing path animation.
<div class="relative">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="198.2" height="188.8" viewBox="0 0 198.2 188.8" style="enable-background:new 0 0 198.2 188.8" xml:space="preserve">
<path id="example-path-03" style="fill:none;stroke:#d7d7d7;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10" d="m99.1 3 29.7 60.2 66.4 9.6-48.1 46.8 11.4 66.2-59.4-31.3-59.4 31.3L51 119.6 3 72.8l66.4-9.6z" />
</svg>
</div>
Options
target (required)
The target option that points the target SVG <path> element is required. All other property options passed in the <> are optional.
<div class="relative">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="198.2" height="188.8" viewBox="0 0 198.2 188.8" style="enable-background:new 0 0 198.2 188.8" xml:space="preserve">
<path id="example-path-02" style="fill:none;stroke:#d7d7d7;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10" d="m99.1 3 29.7 60.2 66.4 9.6-48.1 46.8 11.4 66.2-59.4-31.3-59.4 31.3L51 119.6 3 72.8l66.4-9.6z" />
</svg>
</div>
oriented 3.1.0
Use the oriented: true option to make an element automatically rotate to match the direction of the path it’s following.
<div class="relative">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="198.2" height="188.8" viewBox="0 0 198.2 188.8" style="enable-background:new 0 0 198.2 188.8" xml:space="preserve">
<path id="example-path-02" style="fill:none;stroke:#d7d7d7;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10" d="m99.1 3 29.7 60.2 66.4 9.6-48.1 46.8 11.4 66.2-59.4-31.3-59.4 31.3L51 119.6 3 72.8l66.4-9.6z" />
</svg>
</div>