网页 综合 更新时间 2026-06-03 15:02:38

offset-path | CSS-Tricks

自动蜘蛛 css-tricks.com 正文约 3039 字
offset-path | CSS-Tricks
The offset-path property in CSS defines a movement path for an element to follow during animation.
This property began life as motion-path. This, and all other related motion-* properties, are being renamed offset-* in the spec. We’re changing the names here in the almanac. If you want to use it right now, probably best to use both syntaxes.
Here’s an example using the SVG path syntax:
This property cannot be animated, rather it defines the path for animation. We use the offset-path property to create the animation. Here’s a simple example of animating motion-offset with a @keyframes animation:
In this demo, the orange circle is being animated along the offset-path we set in CSS. We actually drew that path in SVG with the exact same path() data, but that’s not necessary to get the motion.
Say we drew a funky path like this in some SVG editing software:
We would find a path like:
The d attribute value is what we’re after, and we can move it straight to CSS and use it as the offset-path:
Note the unitless values in the path syntax. If you’re applying the CSS to an element within SVG, those coordinate values will use the coordinate system set up within that SVG’s viewBox. If you’re applying the motion to some other HTML element, those values will be pixels.
Also note we used a graphic of a finger pointing to show how the element is automatically rotated so it kinda faces forward. You can control that with offset-rotate:
As best as we can tell, path() and none are the only working values for offset-path.
The offset-path property is supposed to accept all the following values.
Even telling an SVG element to reference a path definied the same SVG via url() doesn’t seem to work.
Dan Wilson explored some of this in Future Use: CSS Motion Paths. You have access to all this same stuff in JavaScript through the Web Animations API. For example, say you’ve defined a offset-path in CSS, you can still control the animation through JavaScript:
Heads up! A lot of these were created before the change from motion-* naming to offset-*. Should be pretty easy to fix them if you’re so inclined.
Our own Sarah Drasner wrote about SMIL, SVG’s native method for animations, and how animateMotion is used to animate objects along a SVG path. It looks like:
GreenSock is another way though. Sarah talks about this in GSAP + SVG for Power Users: Motion Along A Path (SVG not required). Example:
Join our community of front-end nerds!
Andy Clarke shows you how to create designs that people recognize and remember in his brand-new e-book, Standout Designs for the Web. Pre-order it today!
Join our community of front-end nerds! You'll get into the Discord server and member-only perks and free swag.
Sign up to get front-end roundups, browser updates, and exclusive content sent to your inbox bi-weekly.
Get your t-shirts, prints, stickers, and yes, even mousepads at the brand-spankin' new CSS-Tricks store, now open.
CSS-Tricks is powered by a small team of real-life humans.
Handwritten. Twice a month. No spam. See past issues →
访问原文 返回搜索 https://css-tricks.com/almanac/properties/o/offset-path/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。