Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How To Animate Along A Path In CSS

, , or any of the 11 other types that are supported at the time I’m writing this. This way, the browser understands what sort of value it is working with, and when the time arises, it can update the variable’s value for an animation.I’m going to register a custom property called --p, which is short for its syntax, , with an initial value of 10% that will be the “starting” point for the progress indicator.Now, we can use the --p variable where we need it, such as where the hard color stops between red and #eee in the larger circle’s conical gradient that we’re using as the starting point.We want to transition from the custom property’s initial value, 10%, to a larger percentage in order to move the gradient’s hard color stop around the shape. So, let’s set up a CSS transition that is designed to update the value of --p.We’re going to update the value on hover, transitioning from 10% to 80%:One more small adjustment: I like to update the cursor on hover so that it’s clearer what sort of interaction the user is dealing with. In this case, we’re working with something indicating progress, so that’s how we’ll configure it:See the Pen [conic-gradient() animation [forked]](https://codepen.io/smashingmag/pen/mdaKvBe) by Preethi Sam.Our circle is done! We can now hover over the element, and the conical gradient’s hard color stops transitions from 10% to 80% behind the smaller circle that is hiding the rest of the gradient to imply a donut shape. We registered a custom @property with an initial value, applied it to the gradient, and updated the value on hover.The final part of this exercise is to work on the progress indicator. We’re using the gradient to indicate progress, but I want the additional visual aid of an object that travels around the larger circle with the gradient as it transitions values.The idea I had was a little scooter that appears to leave a gradient trail behind it. We already have the circle and the gradient, so all we need is the scooter and a way to make it use the larger circle as a track to drive around.See the Pen [CSS offset animation [forked]](https://codepen.io/smashingmag/pen/NWezoyg) by Preethi Sam.Let’s go ahead and add the scooter to the HTML as an emoji:If we had decided to create the initial donut shape with SVG, then we could have used the same path we used for the larger circle as the track. However, we can still get the same path-making powers in CSS using the offset-path property. It’s so much like writing SVG in CSS that we can actually use the exact same coordinates for an SVG circle in the path():SVG path coordinates are difficult to read, but this is what we’re doing in this particular path:One more thing! We have a starting point for the scooter, thanks to the coordinates in the offset-path. The CSS offset-distance property lets us define the end point where we plan to offset the scooter, which is exactly equal to the --p custom property.We’re already updating our custom --p property on hover to help move the conical gradient’s hard stop position from an initial value of 10% to 80%. We should do the same for the scooter so they move together.I’m using the child combinator (>) since the indicator is a direct child of the circle. If your design includes additional elements or requires the scooter to be a further descendant, then you might consider a general descendant selector instead.Here’s everything we covered in a single CSS snippet. I’ve cleaned things up a tiny bit, such as setting up variables for recurring values, like the --size of the circle.See the Pen [Circular animation with offset Pt. 1 [forked]](https://codepen.io/smashingmag/pen/vYvrwXo) by Preethi Sam.A scooter and a solid gradient are only one idea. How about different objects with different trails?See the Pen [Circular animation with offset Pt. 2 [forked]](https://codepen.io/smashingmag/pen/gOZKJma) by Preethi Sam.I’ve been referring to this component as both a “progress indicator” and a “loader” throughout the article. There is a difference between displaying progress and loading states, but it’s also possible for a loading state to display the loading progress. That’s why I’m using a generic

as a
in the example, but you could just as well use it on more semantic HTML elements, like or depending on your specific use case. For accessibility, you might consider incorporating descriptive text that can be announced as assistive-technology-friendly sentences that describe the data.Let me know if you use this on a project and how you approach it. Share it with me in the comments, and we can compare notes.Tips on front-end & UX, delivered weekly in your inbox. Just the things you can actually use.With practical takeaways, live sessions, video recordings and a friendly Q&A.Everything TypeScript, with code walkthroughs and examples. And other printed books.With a commitment to quality content for the design community.Founded by Vitaly Friedman and Sven Lennartz. 2006–2023.Smashing is proudly running on Netlify, TinaCMS and Swell.Fonts by Latinotype.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How To Animate Along A Path In CSS

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×