{"id":243593,"date":"2016-07-22T10:51:02","date_gmt":"2016-07-22T17:51:02","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=243593"},"modified":"2016-10-19T09:51:01","modified_gmt":"2016-10-19T16:51:01","slug":"offset-distance","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/o\/offset-distance\/","title":{"rendered":"offset-distance"},"content":{"rendered":"
motion-offset<\/code>. This, and all other related motion-* properties, are being renamed offset-* in the spec<\/a>. We’re changing the names here in the almanac. If you want to use it right now, probably best to use both syntaxes.<\/div>\nThe motion-offset<\/code> property in CSS says: how far along the motion-path<\/code><\/a> are you? This is the animatable<\/strong> property associated with motion paths. <\/p>\n.thing-that-moves {\r\n \/* \"Old\" syntax. Available in Blink browsers as of ~October 2015 *\/\r\n motion-path: path('M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0');\r\n motion-offset: 0%;\r\n\r\n \/* Currently spec'd syntax. Should be in stable Chrome as of ~December 2016 *\/\r\n offset-path: path('M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0');\r\n offset-distance: 0%;\r\n \r\n animation: move 3s linear infinite;\r\n}\r\n\r\n\/* Animate the element along the path from 0% to 100% *\/\r\n@keyframes move {\r\n 100% { \r\n motion-offset: 100%; \/* Old *\/\r\n offset-distance: 100%; \/* New *\/\r\n }\r\n}<\/code><\/pre>\nIn the example above, we have set the initial motion-offset<\/code> value at 0%<\/code>, though it’s worth noting that zero is the default value, even when not explicitly defined (we could have left that out).<\/p>\nVariables<\/h3>\n
The offset-distance<\/code> property accepts the following values:<\/p>\n\nlength<\/code><\/li>\npercentage<\/code><\/li>\n<\/ul>\nIn both cases, the value is specifying the length of distance from the starting point of the path (default is 0px<\/code> or 0%<\/code>) to the end point of the path.<\/p>\nExample<\/h3>\n
In this example, we have two circles where one small circle travels along the path of a larger circle.<\/p>\n
Here is the SVG file we are using to draw the shapes:<\/p>\n
<svg viewbox=\"0,0 10,10\" width=\"200px\" height=\"200px\">\r\n \r\n <!-- the circle path to be animated along --> \r\n <path \r\n class=\"track\"\r\n fill=\"none\"\r\n stroke-width=\"0.25\"\r\n d=\"M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0\"\r\n \/>\r\n \r\n <!-- the mover that follows the path -->\r\n <circle class=\"marker\" r=\"1\" fill=\"orange\"><\/circle>\r\n \r\n<\/svg><\/code><\/pre>\nNow, we can set the .marker<\/code> class in our CSS to follow the .track<\/code> class coordinates:<\/p>\n\/* The motion-offset is zero by default *\/\r\n.marker {\r\n offset-path: path('M 5 5 m -4, 0 a 4,4 0 1,0 8,0 a 4,4 0 1,0 -8,0');\r\n animation: move 3s linear infinite;\r\n}\r\n\r\n@keyframes move {\r\n \/* Move the .marker from it's default value of 0% to 100% of the path's length *\/\r\n 100% { offset-distance: 100%; }\r\n}<\/code><\/pre>\nSee the Pen Simple Example of Animating Along a Path<\/a> by Geoff Graham (@geoffgraham<\/a>) on CodePen<\/a>.<\/p>\nSimilarly, we can stop the offset-distance<\/code> value at 50% and the animation would drop off halfway around the path:<\/p>\nSee the Pen Simple Example of Animating Along a Path<\/a> by Geoff Graham (@geoffgraham<\/a>) on CodePen<\/a>.<\/p>\nOr, to control the speed of the animation, we could multiply the offset-distance<\/code> value to 300% to speed things up. However, if we’ve specified the amount of time the animation runs at a value greater than what it takes the element to travel the path, then the movement will stop until the animation has completed its interval before repeating:<\/p>\nSee the Pen Simple Example of Animating Along a Path<\/a> by Geoff Graham (@geoffgraham<\/a>) on CodePen<\/a>.<\/p>\nBrowser support<\/h3>\n
the offset-distance<\/code> property is still considered an experimental feature at the time of this writing and there is no documentation on browser support. However, there is documentation on motion-path<\/code> support and we can use that as a baseline for the time being.<\/p>\nThis browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>Desktop<\/h4>